【发布时间】: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