c#dataset数据更新到数据库
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace DataSet更新
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDataSetUpdate_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users";
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
DataTable table = dataset.Tables[0];
DataRow row = table.Rows[0];
row["Name"] = "newName";
new SqlCommandBuilder(adapter);
adapter.Update(dataset);
}
}
MessageBox.Show("OK!");
}
}
}
为何这些语句执行完后,数据库中第一行的Name列中的内容没有改变呢(数据库连接是正确的,因为事先有测试输出数据库中的内容) 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace DataSet更新
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDataSetUpdate_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users";
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
DataTable table = dataset.Tables[0];
DataRow row = table.Rows[0];
row["Name"] = "newName";
new SqlCommandBuilder(adapter);
adapter.Update(dataset);
}
}
MessageBox.Show("OK!");
}
}
}
为何这些语句执行完后,数据库中第一行的Name列中的内容没有改变呢(数据库连接是正确的,因为事先有测试输出数据库中的内容) 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询