并发冲突:UpdateCommand会影响预期1条记录0。的Visual C#2010 [英] Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. Visual C# 2010

查看:623
本文介绍了并发冲突:UpdateCommand会影响预期1条记录0。的Visual C#2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉。我不会发布此假如我不是没了主意。我用尽了一切的论坛,但都无济于事。



所以我有2个表。两者都是在开始是空的。第一个表是一种像当用户创建一个新的客户档案客户masterlist。这里是代码:

  con.Open(); 
System.Data.SqlClient.SqlCommandBuilder CB;
System.Data.SqlClient.SqlCommandBuilder readingcb;
CB =新System.Data.SqlClient.SqlCommandBuilder(DA);
readingcb =新System.Data.SqlClient.SqlCommandBuilder(readingda);



DataRow的卓尔= custMaster.Tables [custMaster] NEWROW();
的DataRow readingdRow = reading.Tables [阅读] NEWROW()。
卓尔[1] = a_newCust.Text;
readingdRow [9] = a_newCust.Text;
卓尔[2] = b_newCust.Text;
readingdRow [10] = b_newCust.Text;
卓尔[3] = c_newCust.Text;
readingdRow [11] = c_newCust.Text;
卓尔[4] = d_newCust.Text;
readingdRow [0] = d_newCust.Text;
卓尔[5] = e_newCust.Text.ToString();
readingdRow [2] = e_newCust.Text.ToString();
卓尔[6] = f_newCust.Text;
readingdRow [1] = f_newCust.Text;
卓尔[7] = g_newCust.Text.ToString();
如果(radioButton1.Checked ==真)
{
卓尔[8] = radioButton1.Text;
readingdRow [3] = radioButton1.Text;
}
,否则如果(radioButton2.Checked ==真)
{
卓尔[8] = radioButton2.Text;
readingdRow [3] = radioButton2.Text;
}
卓尔[9] =主动;
readingdRow [12] =主动;
readingdRow [4] = 0;
readingdRow [5] = 0;


custMaster.Tables [custMaster] Rows.Add(卓尔)。
reading.Tables [阅读] Rows.Add(readingdRow);


MAXROWS = MAXROWS + 1;
readingMaxRows = readingMaxRows + 1;
INC = MAXROWS - 1;
readinginc = readingMaxRows - 1;


this.da.Update(custMastercustMaster);
this.readingda.Update(阅读,读书);


da.Fill(masterDataSet3.custMaster);
readingda.Fill(streetDataSet.reading);
masterfileGrid.Invalidate();
readingGrid.Invalidate();
masterfileGrid.Refresh();
readingGrid.Refresh();

MessageBox.Show(客户成功地加入!);

con.Close();

这部分完成后,我计划将建立在custMaster表的新纪录,同时也在读书表的新纪录(不过这个记录仍然有一些空字段)。当用户进入到读书表中,他看到了新的记录,然后通过添加一些列值(从而在空字段填写)更新纪录。这里是代码:

  con.Open(); 
System.Data.SqlClient.SqlCommandBuilder readingup;
System.Data.SqlClient.SqlCommandBuilder custpayadd;
readingup =新System.Data.SqlClient.SqlCommandBuilder(readingda);
custpayadd =新System.Data.SqlClient.SqlCommandBuilder(custpayda);
INT测试= int.Parse(f_reading.Text);
INT上一个= int.Parse(readingNinBox.Text);


的DataRow readingdRow = reading.Tables [阅读]行[试验1]。


readingdRow [4] =上一页;
readingdRow [5] = int.Parse(d_reading.Text);
readingdRow [13] = e_reading.Value.ToShortDateString();
readingdRow [6] = int.Parse(d_reading.Text) - 上一个;如果


(g_reading.Text ==住宅)
{
的DataRow resSearchdRow = water.Tables [水。行[int.Parse( d_reading.Text) - 上一页];
readingdRow [7] = resSearchdRow [2];
}


,否则
{
的DataRow commSearchdRow = commWater.Tables [commWater]行[int.Parse(d_reading.Text)。 - 上一页]
readingdRow [7] = commSearchdRow [2];
}

this.readingda.Update(阅读,读书); //>>>>>>>>>>这部分是从哪里获得的并发性错误。

readingda.Fill(streetDataSet.reading);
readingGrid.Invalidate();
readingGrid.Refresh();
//别处:

masterDataSet阅读;

System.Data.SqlClient.SqlDataAdapter readingda;

INT readingMaxRows = 0;

INT readinginc = 0;

this.readingTableAdapter.Fill(this.streetDataSet.reading);

读数=新masterDataSet();

串readingsql =SELECT * FROM读书;

readingda =新System.Data.SqlClient.SqlDataAdapter(readingsql,CON);

readingda.Fill(读书,读书);

readingMaxRows = reading.Tables [阅读] Rows.Count。

readingda.Update(阅读,读书);



我不明白为什么我得到这个,因为我用更新代码相同的路线我其他形式,但他们的工作很好。任何帮助将不胜感激。谢谢



一些额外的信息:我只得到错误的第一次记录被添加到阅读的表。当我关闭并重新打开我的计划,我可以更新记录的罚款。


解决方案

 此.contactDBDataSet.AcceptChanges(); 



这一权利! FIN


i'm sorry. i wouldn't post this if i weren't out of ideas. i've tried everything in the forums, but to no avail.

so i have 2 tables. both are empty at the beginning. the first table is kind of like a customer masterlist where the user creates a new customer profile. here is the code:

        con.Open();
        System.Data.SqlClient.SqlCommandBuilder cb;
        System.Data.SqlClient.SqlCommandBuilder readingcb;
        cb = new System.Data.SqlClient.SqlCommandBuilder(da);
        readingcb = new System.Data.SqlClient.SqlCommandBuilder(readingda);



        DataRow dRow = custMaster.Tables["custMaster"].NewRow();
        DataRow readingdRow = reading.Tables["reading"].NewRow();
        dRow[1] = a_newCust.Text;
        readingdRow[9] = a_newCust.Text;
        dRow[2] = b_newCust.Text;
        readingdRow[10] = b_newCust.Text;
        dRow[3] = c_newCust.Text;
        readingdRow[11] = c_newCust.Text;
        dRow[4] = d_newCust.Text;
        readingdRow[0] = d_newCust.Text;
        dRow[5] = e_newCust.Text.ToString();
        readingdRow[2] = e_newCust.Text.ToString();
        dRow[6] = f_newCust.Text;
        readingdRow[1] = f_newCust.Text;
        dRow[7] = g_newCust.Text.ToString();
        if (radioButton1.Checked == true)
        {
            dRow[8] = radioButton1.Text;
            readingdRow[3] = radioButton1.Text;
        }
        else if (radioButton2.Checked == true)
        {
            dRow[8] = radioButton2.Text;
            readingdRow[3] = radioButton2.Text;
        }
        dRow[9] = "Active";
        readingdRow[12] = "Active";
        readingdRow[4] = 0;
        readingdRow[5] = 0;


        custMaster.Tables["custMaster"].Rows.Add(dRow);
        reading.Tables["reading"].Rows.Add(readingdRow);


        MaxRows = MaxRows + 1;
        readingMaxRows = readingMaxRows + 1;
        inc = MaxRows - 1;
        readinginc = readingMaxRows - 1;


        this.da.Update(custMaster, "custMaster");
        this.readingda.Update(reading, "reading");


        da.Fill(masterDataSet3.custMaster);
        readingda.Fill(streetDataSet.reading);
        masterfileGrid.Invalidate();
        readingGrid.Invalidate();
        masterfileGrid.Refresh();
        readingGrid.Refresh();

        MessageBox.Show("Customer succesfully added!");

        con.Close();

when this part completes, my program will create a new record in the "custMaster" table, and also a new record in the "reading" table (but this record still has some null fields). when the user goes to the "reading" table, he sees the new record, and then updates that record by adding some column values (thereby filling in the null fields). here is the code:

            con.Open();
            System.Data.SqlClient.SqlCommandBuilder readingup;
            System.Data.SqlClient.SqlCommandBuilder custpayadd;
            readingup = new System.Data.SqlClient.SqlCommandBuilder(readingda);
            custpayadd = new System.Data.SqlClient.SqlCommandBuilder(custpayda);
            int test = int.Parse(f_reading.Text);
            int prev = int.Parse(readingNinBox.Text);              


            DataRow readingdRow = reading.Tables["reading"].Rows[test-1];                  


            readingdRow[4] = prev;
            readingdRow[5] = int.Parse(d_reading.Text);
            readingdRow[13] = e_reading.Value.ToShortDateString();
            readingdRow[6] = int.Parse(d_reading.Text) - prev;


            if (g_reading.Text == "Residential")
            {
                DataRow resSearchdRow = water.Tables["water"].Rows[int.Parse(d_reading.Text) - prev];
                readingdRow[7] = resSearchdRow[2];
            }


            else
            {
                DataRow commSearchdRow = commWater.Tables["commWater"].Rows[int.Parse(d_reading.Text) - prev];
                readingdRow[7] = commSearchdRow[2];
            }               

            this.readingda.Update(reading, "reading"); //>>>>>>>>>>this part is where i get the concurrency error.

            readingda.Fill(streetDataSet.reading);                
            readingGrid.Invalidate();
            readingGrid.Refresh();
//elsewhere:

masterDataSet reading;

System.Data.SqlClient.SqlDataAdapter readingda;

int readingMaxRows = 0;

int readinginc = 0;

this.readingTableAdapter.Fill(this.streetDataSet.reading);

reading = new masterDataSet();

string readingsql = "SELECT * From reading";

readingda = new System.Data.SqlClient.SqlDataAdapter(readingsql, con);

readingda.Fill(reading, "reading");

readingMaxRows = reading.Tables["reading"].Rows.Count;

readingda.Update(reading, "reading");

i don't understand why i get this, since i use the same lines of updating codes in my other forms but they work well. any help would be greatly appreciated. thanks.

Some additional information: i only get the error the first time a record is added to the "reading" table. when i close and reopen my program, i can update the record fine.

解决方案

this.contactDBDataSet.AcceptChanges();

That Right !! FIN

这篇关于并发冲突:UpdateCommand会影响预期1条记录0。的Visual C#2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆