Update database with changes made to DataTable… confusion

前端 未结 2 1647
轮回少年
轮回少年 2020-12-09 05:57

If I fill a DataTable with DataAdapter.Fill(DataTable); and then make changes to a row in the DataTable with something simple like this: DataTable.Rows[0]

2条回答
  •  隐瞒了意图╮
    2020-12-09 06:30

    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);
    

提交回复
热议问题