当前位置:Gxlcms > 数据库问题 > SqlDataAdapter.Update()方法与SqlCommandBuilder(转)

SqlDataAdapter.Update()方法与SqlCommandBuilder(转)

时间:2021-07-01 10:21:17 帮助过:3人阅读

strcon1 = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"; using (SqlConnection conn = new SqlConnection(strcon1)) { conn.Open(); string strsql = "select * from employees"; SqlCommand cmd = new SqlCommand(strsql, conn); ad = new SqlDataAdapter(); ad.SelectCommand = cmd; dt = new DataTable("employees"); ad.Fill(dt); ds = new DataSet(); ds.Tables.Add(dt); this.dataGridView1.DataSource = ds.Tables[employees];

 


增加一行记录:
//新增加一行

                DataRow newRow = dt.NewRow();
                newRow["EmployeeID"] = 11;
                newRow["LastName"] = "Bill";
                newRow["FirstName"] = "Gata";
                dt.Rows.Add(newRow);

 


更新:用Update更新时提示要有SqlDataAdapter的InsertCommand ,这里我们可以用SqlCommandBuilder自动为我们生成

                //使用SqlCommandBuilder自动生成带有Insert语句的InsertCommand
                SqlCommandBuilder cb = new SqlCommandBuilder(ad);
                ad.InsertCommand = cb.GetInsertCommand();
                //将DataSet中的数据更新到数据库中
                ad.Update(ds, "employees");

 


}

 

在CSDN博客上看到的,感觉不错,就转过来了。

人气教程排行