窗口只有1个datagridview和一个button1,datagidview编辑数据,button1确定更新
当插入新数据的时候,可以成功插入,当编辑现有数据update的时候,一点更新就报错。提示:“Concurrency violation: the DeleteCommand affected 0 of the expected 1 records.”
我根本不知道错在哪里了。希望指点代码如下
    public partial class FormAccess : Form
    {
        OleDbConnection connect = new OleDbConnection(@";Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ESDB.accdb");
        DataTable dt=new DataTable ();
        OleDbDataAdapter adapter;
        public FormAccess()
        {
            InitializeComponent();
        }        private void FormAccess_Load(object sender, EventArgs e)
        {
            connect.Open();
            adapter = new OleDbDataAdapter();
            string selectText = "select * from factory";
            DbCommand selectCommand = new OleDbCommand(selectText, connect);            
            adapter.SelectCommand =(OleDbCommand ) selectCommand;
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
            string insertText="insert into factory (factoryCode,factoryName,description) values(@factoryCode,@factoryName,@description)";
            string updateText = "update factory set factoryName=@factoryname,description=@description where factoryCode=@factoryCode";
            OleDbCommand insertCommand = new OleDbCommand(insertText, connect);
            insertCommand.Parameters.Add("@factoryCode", OleDbType.VarWChar, 0, "factoryCode");
            insertCommand.Parameters.Add("@factoryName", OleDbType.VarWChar, 0, "factoryName");
            insertCommand.Parameters.Add("@description", OleDbType.VarWChar, 0, "description");            OleDbCommand updateCommand = new OleDbCommand(updateText, connect);
            updateCommand.Parameters.Add("@factoryCode", OleDbType.VarWChar, 0, "factoryCode");
            updateCommand.Parameters.Add("@factoryName", OleDbType.VarWChar, 0, "factoryName");
            updateCommand.Parameters.Add("@description", OleDbType.VarWChar, 0, "description");            adapter.UpdateCommand = (OleDbCommand)updateCommand;
            adapter.InsertCommand = (OleDbCommand)insertCommand;
        }        private void button1_Click(object sender, EventArgs e)
        {
            adapter.Update(dt);
        }

解决方案 »

  1.   

    更新应该是更新对应的数据你的button是放在每一列里面的  每一列后面有个Button?
    这样的话你的button估计要在DataItemBoud里面写了
    最好是用linkbutton
      

  2.   

    批量更新当然是只有1个button 啊。不会每个列都会放
      

  3.   

    http://liujace.itpub.net/post/4284/241796
      

  4.   

    把更新语句的参数改一下试试,加"@Original_"
    update factory set factoryName=@factoryname,description=@description where factoryCode=@Original_factoryCode";http://www.mybuffet.cn
      

  5.   

    违反并发性: DeleteCommand 影响了预期1 条记录中的0 条
    Update(ds);   
    ds.AcceptChanges();
    检查是否设有主键
    DeleteCommand的问题:检查是否含有自动编号字段
    UpdateCommand的问题:检查更新的字段的原始值是否为空值
    http://topic.csdn.net/u/20070729/11/9ff501ec-139c-48de-8707-0e2c69327674.html
      

  6.   

    ACCESS数据库,很可能是主键问题. 加上主键
      

  7.   

    insert 时 factoryCode 你怎么处理的,如果不是自增列,应该赋值,否则主键为空出错.insert 到数据库的(自增)主键,返回到DataTable中,否则更新时主键没有对应关系.