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),哪怕你这个表是在程序中建立的,数据不是从数据库中提取的也能够,之前没想好好研究,如今理解了写出来,也供有须要的朋友参考,若是有不正确的地方,也请多指教,谢谢^_*