private static DataTable dt;
private static OleDbDataAdapter da;
rotected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindgridview2();
}
private void bindgridview2()
{
dt = new DataTable();
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\lx.mdb");
da = new OleDbDataAdapter("select * from lx", conn);
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
protected void updat()
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=D:\\lx.mdb");
da = new OleDbDataAdapter("select * from lx", conn);
OleDbCommand cmd = new OleDbCommand("delete * from lx where id = @id", conn);
cmd.Parameters.Add("@id", OleDbType.Integer, 4, "id");
da.DeleteCommand = cmd;
cmd = new OleDbCommand("update lx set where ", conn);
cmd.Parameters.Add("@txt", OleDbType.VarChar, 50, "txt");
cmd.Parameters.Add("@id", OleDbType.Integer, 4, "id");
da.UpdateCommand = cmd;
da.Update(dt);
}
如果编译器报错“违反并发性: UpdateCommand 影响了预期 1 条记录中的 0 条。”,有可能是以下几种情况
1.数据库中没有定义主键。
2.更新数据库的时候在datatable表中误修改了主键的值。
3.字段类型不一致或字段不一致