c# 中的DataTable与SQL Server中的Table比较,将DataTable中新增的DataRow更新到Table中去

用SqlDataAdapter怎么做
2024-10-31 06:14:52
推荐回答(1个)
回答(1):

这个感觉很简单,但是要借助datagridview数据组件,用CommandBuilder对象进行更新,可以实现删除、更新、或者添加操作。比如,以sqlite数据库(与sqlserver数据库类似,体量小一些)为例:
static SQLiteConnection conn = new SQLiteConnection("Data Source=F:\\Project\\Data\\test;Version=3;");
SQLiteCommand cmd = new SQLiteCommand("select * from usertab", conn);
SQLiteDataAdapter sda = new SQLiteDataAdapter();
DataSet ds=new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(ds,"usertab");
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "usertab";
conn.Close();

}
private void button1_Click(object sender, System.EventArgs e)
{
SQLiteCommandBuilder scb = new SQLiteCommandBuilder(sda);
scb.GetUpdateCommand();
sda.Update(ds,"usertab");
}