当一次性更新大量字段时,用sql语句不太方便,在内存中建立虚拟表,与数据库中表和字段一一对应,是一种方法。
protected void btnSave_Click(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter adp = new SqlDataAdapter("select * from student where ID='001'", conn);
DataSet ds = new DataSet();
adp.Fill(ds, "student");
DataTable dt = ds.Tables["student"];
DataRow dr;
dr = dt.Rows[0];
dr["ID"] = this.ID.Text;
dr["name"] = this.name.Text;
SqlCommandBuilder cmb = new SqlCommandBuilder(adp);//核心代码
adp.InsertCommand = cmb.GetInsertCommand();
adp.UpdateCommand = cmb.GetUpdateCommand();
adp.DeleteCommand = cmb.GetDeleteCommand();
adp.Update(dt);
}