Edited dataset do not update the database and return a concurrency violation error (C#)
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
开发者_如何学C }
}
//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
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.
精彩评论