System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter();//定義一個SqlDataAdapter 的實例 數據庫
SqlCommand scmd = new SqlCommand();//定義一個SqlCommand的實例,以便綁定
scmd.CommandType = CommandType.Text;
scmd.Connection = new SqlConnection(ConnectionString); spa
scmd.Parameters.Add("@acc_title_id", SqlDbType.Int, 4, "acc_title_id");//重要的地方,將dt的列寫到參數中去
scmd.Parameters.Add("@acc_std_title_id", SqlDbType.Int, 4, "acc_std_title_id");
scmd.Parameters.Add("@back_id", SqlDbType.Int, 4, "back_id"); orm
scmd.CommandText = string.Format("update t_std_acc_title_ent_acc_title set where acc_std_title_id = @acc_std_title_id and acc_id = {0} and ", eleid);
sda.UpdateCommand = scmd;//更新語句 內存
scmd = new SqlCommand();
scmd.CommandType = CommandType.Text;
scmd.Connection = new SqlConnection(ConnectionString); cmd
scmd.Parameters.Add("@acc_title_id", SqlDbType.Int, 4, "acc_title_id");
scmd.Parameters.Add("@acc_std_title_id", SqlDbType.Int, 4, "acc_std_title_id"); string
scmd.CommandText = string.Format("insert into t_std_acc_title_ent_acc_title values(@acc_std_title_id,{0},@acc_title_id)",eleid); it
sda.InsertCommand = scmd;//插入語句 io
sda.Update(dt.Select(null, null, DataViewRowState.ModifiedCurrent));//先執行更新 table
sda.Update(dt.Select(null, null, DataViewRowState.Added));//再執行插入 class
以上咱們就進行了一個數據庫的更新,它能夠更新你任何一個表(內存中的DataTable),哪怕你這個表是在程序中建立的,數據不是從數據庫中提取的也能夠,之前沒想好好研究,如今理解了寫出來,也供有須要的朋友參考,若是有不正確的地方,也請多指教,謝謝^_*