【问题标题】:Edited dataset do not update the database and return a concurrency violation error (C#)编辑的数据集不更新数据库并返回并发冲突错误(C#)
【发布时间】:2011-09-13 10:40:37
【问题描述】:

我正在同步来自 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 中的数据吗?我用 Data vizualiser 检查了合并后里面有 21 行(与 sybase 数据库中相同)。

干杯, 磅

【问题讨论】:

    标签: c# sql-server concurrency synchronization sybase


    【解决方案1】:

    【讨论】:

    • 我在网上查了很多帖子,包括这个。我并没有真正明白... :(我的 SQL 服务器表是空的,但我不在服务器上而是在数据集上执行直接 SQL 请求。我希望在 dataadpater 更新期间它将所有 21 行插入数据库...
    • 我认为数据集的乐观并发对我来说是可以的,因为在这种情况下我正在更新整行,但我不明白为什么它没有插入行...可能是因为我数据库中有两个主键?
    • 这可能是问题所在,如果其中一个关键字段可以通过合并进行更改。
    • 合并更改了所有 dspms 数据集,因为它之前是空的。你认为这是一个问题吗?
    • 如果主键字段之一被更改 - 肯定会。
    猜你喜欢
    • 2014-04-24
    • 2010-12-18
    • 2018-07-10
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多