编辑的数据集不会更新数据库并返回并发冲突错误 (C#)

发布于 2024-12-04 16:53:43 字数 2748 浏览 0 评论 0原文

我正在同步 Sybase 数据库中不同表(大约 20 个表)的数据,我通过 ODBC 和 SQL Server(我的该项目的主要数据库)访问该数据库。

当我启动同步数据的函数时(目前从 Sybase 到 SQLServer 只有一种方式),没有任何反应,并且出现并发冲突错误:“并发冲突:UpdateCommand 影响了预期 1 条记录中的 0 条。”

SQL Server 上的表有 96 列,而 Sybase 上的表只有 94 列。我在 SQL Server 表中有两列用于同步目的。第一次同步前SQL Server表为空,但sybase数据库已有21行。

您可以查看下面的代码以更好地理解它:

            try
            {
            //Connect to the erp Sybase database through ODBC
            OdbcConnection myConnection;
            //OdbcCommand myCommand;
            string MySQLRequest = "SELECT * FROM toto";
            string tableName = "toto";

            myConnection = new OdbcConnection("dsn=blabla;UID=bla;PWD=bla;");  
            //settings of the current database
            myConnection.Open();


            //get the data from MangoERP and store it into a dataset
            OdbcDataAdapter dasyb = new OdbcDataAdapter(MySQLRequest, myConnection);
            DataSet dserp = new DataSet(); //94columns
            dasyb.Fill(dserp, tableName);
            int dserpcount=dserp.Tables[0].Rows.Count;

            //get the data from MangoPMS and store it into a dataset
            SqlDataAdapter dasql = new SqlDataAdapter(MySQLRequest, mango_pms.Properties.Settings.Default.ConnectionStringSQLServer);
            DataSet dspms = new DataSet();//96columns
            dasql.Fill(dspms, tableName);
            int dspmscount = dspms.Tables[0].Rows.Count;

            //merge the dataset together
            if (dserpcount > 0)
            {
                dspms.Tables[0].Merge(dserp.Tables[0], false, MissingSchemaAction.Ignore); 
                dspmscount = dspms.Tables[0].Rows.Count;
            }
            string dtutcnow=DateTime.UtcNow.ToString();

            for (int i = 0; i < dspmscount; i++)
            {
                if (dspms.Tables[0].Rows[i]["erpsyncdate"].ToString() == "")
                {
                    dspms.Tables[0].Rows[i]["erpsyncdate"] = dtutcnow;
                }
                if (dspms.Tables[0].Rows[i]["erpsync"].ToString() == "")
                {
                    dspms.Tables[0].Rows[i]["erpsync"] = 2; //1=pms, 2=erp  
                }
            }

            //create an SqlCommandBuilder
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dasql); 

            //save back to pms table
            dasql.Update(dspms, tableName);

            //Close the connections
            dasyb.Dispose();
            dasql.Dispose();
            myConnection.Close();

        }
        catch
        {
            MessageBox.Show("Can not synchronize from ERP database!");
        }

您知道为什么我无法保存数据集 dspms 中的数据吗?我用数据可视化器检查了合并后内部有 21 行(与 sybase 数据库中相同)。

干杯, 磅

I am synchronizing data from different tables (about 20 tables) from Sybase database which I access through ODBC and a SQL Server (my main database for this project).

When I launch the function to sync the data (only one way for the moment from Sybase to SQLServer), nothing happen and i get a concurrency violation error: "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."

The table on the SQL server has 96 columns and the one on Sybase only 94. I had two columns in the SQL Server table for synchronization purpose. The SQL server table is empty before the synchronization the first time but the sybase database has already 21 rows.

You may have a look to the code below to understand it better:

            try
            {
            //Connect to the erp Sybase database through ODBC
            OdbcConnection myConnection;
            //OdbcCommand myCommand;
            string MySQLRequest = "SELECT * FROM toto";
            string tableName = "toto";

            myConnection = new OdbcConnection("dsn=blabla;UID=bla;PWD=bla;");  
            //settings of the current database
            myConnection.Open();


            //get the data from MangoERP and store it into a dataset
            OdbcDataAdapter dasyb = new OdbcDataAdapter(MySQLRequest, myConnection);
            DataSet dserp = new DataSet(); //94columns
            dasyb.Fill(dserp, tableName);
            int dserpcount=dserp.Tables[0].Rows.Count;

            //get the data from MangoPMS and store it into a dataset
            SqlDataAdapter dasql = new SqlDataAdapter(MySQLRequest, mango_pms.Properties.Settings.Default.ConnectionStringSQLServer);
            DataSet dspms = new DataSet();//96columns
            dasql.Fill(dspms, tableName);
            int dspmscount = dspms.Tables[0].Rows.Count;

            //merge the dataset together
            if (dserpcount > 0)
            {
                dspms.Tables[0].Merge(dserp.Tables[0], false, MissingSchemaAction.Ignore); 
                dspmscount = dspms.Tables[0].Rows.Count;
            }
            string dtutcnow=DateTime.UtcNow.ToString();

            for (int i = 0; i < dspmscount; i++)
            {
                if (dspms.Tables[0].Rows[i]["erpsyncdate"].ToString() == "")
                {
                    dspms.Tables[0].Rows[i]["erpsyncdate"] = dtutcnow;
                }
                if (dspms.Tables[0].Rows[i]["erpsync"].ToString() == "")
                {
                    dspms.Tables[0].Rows[i]["erpsync"] = 2; //1=pms, 2=erp  
                }
            }

            //create an SqlCommandBuilder
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dasql); 

            //save back to pms table
            dasql.Update(dspms, tableName);

            //Close the connections
            dasyb.Dispose();
            dasql.Dispose();
            myConnection.Close();

        }
        catch
        {
            MessageBox.Show("Can not synchronize from ERP database!");
        }

Do you have any idea why I can not save the data from the dataset dspms. I checked with the Data vizualiser there is 21 rows inside (same as in the sybase databse) after merging.

Cheers,
LB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2024-12-11 16:53:43

这似乎是因为数据集的默认值......
查看 并发冲突:UpdateCommand 影响了预期 1 条记录中的 0 条。

This seems to be because of a Default value of the DataSet...
Look at Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文