且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新数据库,并对DataTable ...进行了修改

更新时间:2023-12-01 11:01:34

这是一个实际有用的答案,以防其他人需要知道如何做到这一点:

Here is an actual helpful answer in case anyone else needs to know how to do this:

string selectStatement = "SELECT * FROM Contact";
System.Data.DataTable dt = new System.Data.DataTable();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = new SqlCommand(selectStatement, conn);
SqlCommandBuilder cb = new SqlCommandBuilder(sqlDa);
sqlDa.Fill(dt);
dt.Rows[0]["Name"] = "Some new data here";
sqlDa.UpdateCommand = cb.GetUpdateCommand();
sqlDa.Update(dt);