c#修改数据库数据错误
c#连接mysql数据库在datagridview里显示数据.查询、删除、新增都没有问题,但是修改数据时出现错误:
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records
不能对查询出来的数据修改,只能对新增加的数据修改,不知道什么原因。
下面代码里的sql对应的是查询语句。
代码:
private void btnSave_Click(object sender, EventArgs e)
{
myadp = MySqlDBUtil.ExecuteDatapter(sql, "libs_bz");
mycb = new MySqlCommandBuilder(myadp);
try
{
myadp.Update(ds.Tables["libs_bz"]);
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message);
}
bindingSourceData.DataSource = ds.Tables["libs_bz"];
bindingNavigatorData.BindingSource = bindingSourceData;
dataGridViewData.DataSource = bindingSourceData;
}
ExcuteDatapter代码:
public static MySqlDataAdapter ExecuteDatapter(string sql, string sqlda)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
MySqlDataAdapter da;
try
{
connection.Open();
da = new MySqlDataAdapter(sql, connection);
//da.FillSchema(ds,SchemaType.Mapped);
dt = ds.Tables[sqlda];
da.Fill(ds, sqlda);
}
catch (Exception ex)
{
throw ex;
}
return da;
}
}
拜托各位高人指教~ --------------------编程问答-------------------- http://www.cnblogs.com/JohnXIe/archive/2008/11/06/1328305.html
补充:.NET技术 , C#