简体   繁体   English

当我从客户端应用程序调用sql存储过程时,是否可能引发DBCurrencyException?

[英]Is it possible to raise a DBCurrencyException when Im calling sql stored procedures from my client app?

I've got a task (Im a student). 我有一个任务(我是学生)。 I need to call a DBCurrencyException. 我需要调用DBCurrencyException。 Is it possible to raise a DBCurrencyException when Im calling sql stored procedures from my client app that changes database data? 当我从更改数据库数据的客户端应用中调用sql存储过程时,是否可能引发DBCurrencyException? is it possible? 可能吗?

I'm not sure about your problem due to you don't provide your code. 由于您未提供代码,因此我不确定您的问题。

but.... I hope this article can help you http://msdn.microsoft.com/en-us/library/ms171936%28v=vs.110%29.aspx 但是...。希望本文能对您有所帮助http://msdn.microsoft.com/zh-cn/library/ms171936%28v=vs.110%29.aspx

An ADO.Net Dataset uses Optimistic Concurrency by default. 默认情况下,ADO.Net数据集使用乐观并发。 If an attempt is made to update a row to the database when the row no longer exists in the database then the result is the error: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. 如果在数据库中不再存在该行时尝试将行更新到数据库,则结果为错误:并发冲突:UpdateCommand影响了预期的1条记录中的0条。

Here is the scenario where that error can occur. 在这种情况下,可能会发生该错误。

// data row has been added 
DataRow dr= null;

dtTab = (DataTable)Session("dtTab");

dr = dtTab.Rows(e.RowIndex);

dr.Delete();

dtTab.AcceptChanges();


// If Acceptchanges() being not called, row status will be 
//detached, that will not be updated to database.

//Without updating database Acceptchanges() called.Row status 
//changed to deleted. If this update to database, 
// it will give concurrency error-   
// because row no longer exist in database. 

The reason for the error became apparent. 错误的原因显而易见。 The records in the row in the table didn't exist in the database. 表中的行中的记录在数据库中不存在。 However, the DeleteCommand was trying to remove them from the Database. 但是,DeleteCommand试图从数据库中删除它们。 And when the data adapter class attempts to delete a record and doesn't see any rows being changed, it assumes that a concurrency violation has occured. 并且,当数据适配器类尝试删除一条记录并且看不到任何行被更改时,它假定发生了并发冲突。

Here is the artcile (The Source of above code) regarding the issue. 这是关于该问题的文章 (上述代码的来源)。 This article describe when the DBConcurrencyException will raised and how to solve the problem. 本文介绍何时引发DBConcurrencyException以及如何解决该问题。 Please go through the artcicle. 请通过关节。

Please try the same things with store procedure, may this will help you. 请尝试与存储过程相同的操作,可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM