本文介绍了C#SQL Express2010.在同一运行中添加和更新记录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用数据库的完全菜鸟,所以我认为我的问题很琐碎.我仍然无法找到谷歌搜索添加和更新"的答案.我的问题是,正如标题所示,我无法更新在程序的同一运行中添加的记录.

请遵守代码:

I''m a complete noob to working with databases so I assume my question is trivial. Still I wasn''t able to find an answer googling "add and update". My problem is, as the title suggests, that I''m unable to update records which I add in the same run of the program.

Please observe the code:

private static System.Data.SqlClient.SqlDataAdapter da_small;
private static DataSet ds_small;

public static void add()
        {
            System.Data.SqlClient.SqlCommandBuilder cb_small;
            cb_small = new System.Data.SqlClient.SqlCommandBuilder(da_small);

            DataRow small_short = ds_small.Tables["small"].NewRow();

            small_short[1] = "just added";

            for (int i = 2; i < 36; i++)
                small_short[i] = i.ToString();

            ds_small.Tables["small"].Rows.Add(small_short);
            
            da_small.Update(ds_small, "small");
        }

        private static int i = 0;
        
        public static void update()
        {
            System.Data.SqlClient.SqlCommandBuilder cb_small;
            cb_small = new System.Data.SqlClient.SqlCommandBuilder(da_small);

            //DataRow[] drs = ds_small.Tables["small"].Select("PlayerID=''" + "just added" + "''");

            System.Data.DataRow dr = ds_small.Tables["small"].Rows[i++];
            dr[2] = "changed";
            da_small.Update(ds_small, "small");
        }



使用2个add()调用,然后1个update()调用运行时,在调用update()时收到以下运行时异常:

并发冲突:UpdateCommand影响了预期的1条记录中的0条"

当通过调用add()和3次调用update()来第二次运行它时,我在第三次调用update()时遇到了这个异常,这意味着在上一次运行中添加的记录已成功更新,而新添加的记录无法更新.

我假设问题是适配器尚未意识到添加的记录,我只是对SQL Server Express不够(或根本不熟悉)以了解为什么或不知道如何解决它.

请帮助,谢谢.



When running this with 2 calls to add(), then 1 call to update() I receive the following run time exception when calling update():

"Concurrency violation: the UpdateCommand affected 0 of the expected 1 records"

When running this for the second time with call to add() and 3 calls to update() I''m getting this exception on the third call to update(), meaning that the records which were added on the previous run were updated successfully, while the newly added record couldn''t be updated.

I''m assuming that the problem is that the adapter isn''t aware of the added record yet, I''m just not familiar enough (or at all) with SQL server express to understand why or to know how to fix it.

Please help, thank you.

推荐答案

UPDATE MyTable
SET    Field1 = bindedNewValue1, 
       Field2 = bindedNewValue2, 
       Field3 = bindedNewValue3
WHERE Field1 = bindedOldValue1
AND   Field2 = bindedOldValue2
AND   Field3 = bindedOldValue3


现在,如果数据库中的值已以某种方式更改,则更新将找不到任何要修改的记录,从而导致您收到错误.修改可能已由您的程序,触发器等完成.

检查数据表中的值以查看原始版本和新版本.您可以使用例如DataTable的GetChanges方法来获取先前的情况.您还可以连接适配器的RowUpdating事件,以在更新之前更轻松地查看信息.


Now if the values in the database have changed in some way the update won''t find any records to modify resulting to the error you received. The modification could have been done by your program, trigger etc.

Check the values from the datatable to see the original and the new versions. You can use for example DataTable''s GetChanges method to get the previous situation. You can also hook up to the adapter''s RowUpdating event to see more easily the information before the update is made.


这篇关于C#SQL Express2010.在同一运行中添加和更新记录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:38