并发冲突,updatecommand影响了预期的0 [英] concurrency violation the updatecommand affected 0 of the expected

查看:96
本文介绍了并发冲突,updatecommand影响了预期的0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在更新两个不同的表时遇到问题,这两个表位于相同数据库中,也位于不同数据库中.
当我使用以下代码时,它不会更新第二张表并说

并发冲突,updatecommand影响了预期错误的0 

请注意,在以下代码中,我正在更新网格一列并更新数据库.

"mySymbolMastertable"表已经填充了更改.
请帮助...

 私有 无效

UpdateDatabases()

{

strAddResult =  StringBuilder();

尝试

{

DataTable dtsec =  DataTable();

dtsec = mySymbolMastertable.GetChanges();

 如果(connList!= )

{

 for ( int  i =  0 ; i <  connList.Count; i ++)

{

 字符串 connectionName;

 字符串 sqlString;

 DataTable dt =  DataTable

();

connectionName = connList [i] .ToString();

connectionString = System.Configuration.ConfigurationSettings .AppSettings [connectionName];

 SqlConnection mySQLConnection =  SqlConnection(connectionString);

 如果(连接名称== "  )

{

sqlString = " ;

}

 其他

{

sqlString = " ;

}

 SqlDataAdapter myDataAdapter =  SqlDataAdapter(sqlString,mySQLConnection);

 SqlCommandBuilder myCmdBuilder =  SqlCommandBuilder(myDataAdapter);

 dt = dtsec;

 //  dt = mySymbolMastertable.GetChanges(); 

 // 添加处理程序

 //  myDataAdapter.RowUpdating + =新的SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating); 

 myDataAdapter.RowUpdated + =  SqlRowUpdatedEventHandler(myDataAdapter_RowUpdated);

myDataAdapter.ContinueUpdateOnError =  true ;

 //  myDataAdapter.InsertCommand = myCmdBuilder.GetInsertCommand(); 

 //  myDataAdapter.UpdateCommand = myCmdBuilder.GetUpdateCommand(); 

 myDataAdapter.Update(dt);

 // 添加处理程序

 //  myDataAdapter.RowUpdating + =新的SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating); 

myDataAdapter.RowUpdated + =  SqlRowUpdatedEventHandler(myDataAdapter_RowUpdated);

 myCmdBuilder = ;

myDataAdapter = ;

mySQLConnection = ;

 }}

}

捕获(例外)

{

' '捕获异常

}} 


请帮助...

解决方案

当我们使用CommandBuilder并使用DataAdapter的更新方法时,连接将自动打开和关闭. br/>
正如我所说的,这是第一次更新,所以我认为连接还可以.

如果您发现其他内容,请告诉我


调用GetChanges()时,将获得主表更改后的行的新副本(mySymbolMastertable).每次GetChanges调用均返回相同的内容除非您将mySymbolMastertable更改为table(dtSec).将dtSec分配为dt不会执行任何操作.您仍然更新dtSec.也许您可以调用dtSec.Copy()来不为下一次Update调用更新dtSec.for循环中已获取GetChanges()解决了您遇到问题了吗?


当您尝试更新表中的一行(在您的情况下为dtsec),但该行在原始数据库中不存在时会发生这种情况.行或从数据库中更改行,因为您持有的行与db不匹配,所以您无法更新该行.请确保dtsec中的所有行都存在于第二个数据库中.您可以阅读Ado.Net Optimistic Concurrency模型.使用乐观并发更新行时,必须与数据库中的值匹配的值(我的意思是值是所有列在一行中,单元格值)


Hi,

I have an issue with updating two different table which are in same and also in different database.
When I use the following code, it is not updating second table and saying

concurrency violation the updatecommand affected 0 of the expected error

Please note in following code I am updating Grid one column and updating the Database.

"mySymbolMastertable" table is already populated with changes.
PLEASE HELP...

private void 

UpdateDatabases() 

{

strAddResult =new StringBuilder (); 

try

{

DataTable dtsec= new DataTable () ; 

dtsec= mySymbolMastertable.GetChanges();

 if (connList != null ) 

{ 

for (int i = 0; i < connList.Count; i++) 

{

 string connectionName; 

 string sqlString; 

 DataTable dt = new DataTable 

(); 

connectionName = connList[i].ToString();

connectionString = System.Configuration.ConfigurationSettings .AppSettings[connectionName]; 

 SqlConnection mySQLConnection = new SqlConnection (connectionString);  

 if (connectionName == "SecMasterConnectionSec" ) 

{

sqlString = "SELECT * FROM [SymbolMaster_Secondary]" ; 

}

 else 

{

sqlString ="SELECT * FROM [SymbolMaster]" ; 

} 

 SqlDataAdapter myDataAdapter = new SqlDataAdapter (sqlString, mySQLConnection); 

 SqlCommandBuilder myCmdBuilder = new SqlCommandBuilder (myDataAdapter); 

 dt = dtsec; 

 // dt = mySymbolMastertable.GetChanges();  

 //Add Handler

 //myDataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating);

 myDataAdapter.RowUpdated +=new SqlRowUpdatedEventHandler (myDataAdapter_RowUpdated); 

myDataAdapter.ContinueUpdateOnError =true ; 

 //myDataAdapter.InsertCommand = myCmdBuilder.GetInsertCommand();

 //myDataAdapter.UpdateCommand = myCmdBuilder.GetUpdateCommand();

 myDataAdapter.Update(dt);

 //Add Handler

 // myDataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating);

myDataAdapter.RowUpdated +=new SqlRowUpdatedEventHandler (myDataAdapter_RowUpdated); 

 myCmdBuilder =null ; 

myDataAdapter =null; 

mySQLConnection =null ; 

 }}

} 

catch (Exception ex) 

{  

''Catching Exception 

}}


PLEASE HELP...

解决方案

When we are using CommandBuilder and using update method of DataAdapter so connection will automatically open and close.

As I said, it is updating first time so I think connection is ok.

Please let me know if you find something else


When you call GetChanges(), you get a new copy of main table''s changed rows(mySymbolMastertable) .Every GetChanges call returns the same table(dtSec) unless you change mySymbolMastertable.Assigning dtSec to dt does nothing.You update still dtSec.Maybe you can call dtSec.Copy() to not to update dtSec for next Update call.Has getting GetChanges() in your for loop solved your problem?


This occurs when you try to update a row in a table(dtsec in your case) but that row doesn''t exist in original database.Simply if another procedure delete a row or change a row from database,you can''t update that row since the row you hold isn''t matching with db..Be sure that all row in dtsec exist in second database.You can read Ado.Net Optimistic Concurrency model.When updating a row using optimistic concurrency ,the value you have has to match value in database (What I mean by value is all columns in a row ,cell values)


这篇关于并发冲突,updatecommand影响了预期的0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆