【问题标题】:Is it possible to raise a DBCurrencyException when Im calling sql stored procedures from my client app?当我从客户端应用程序调用 sql 存储过程时,是否可以引发 DBS 货币异常?
【发布时间】:2013-10-28 16:21:39
【问题描述】:

我有一个任务(我是学生)。我需要调用 DBCurrencyException。当我从更改数据库数据的客户端应用程序调用 sql 存储过程时,是否可以引发 DBCurrencyException?有可能吗?

【问题讨论】:

  • DbCurrencyException 是您创建的类吗?然后在需要时执行throw new DbCurrencyException。如果您需要进一步的帮助,您需要发布给您带来麻烦的代码。
  • 我有一个任务。我必须提出这个例外。

标签: c# sql sql-server visual-studio visual-studio-2012


【解决方案1】:

ADO.Net 数据集默认使用乐观并发。如果在数据库中不再存在该行时尝试将行更新到数据库,则结果是错误:并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条。

这是可能发生该错误的情况。

// 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. 

错误的原因很明显。表中的行中的记录在数据库中不存在。但是,DeleteCommand 试图将它们从数据库中删除。并且当数据适配器类尝试删除记录并且没有看到任何行被更改时,它假定发生了并发冲突。

这是关于该问题的artcile(上述代码的来源)。这篇文章描述 何时会引发 DBConcurrencyException 以及如何解决 问题。请仔细阅读文章。

请在存储过程中尝试相同的操作,希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2022-11-05
    • 1970-01-01
    相关资源
    最近更新 更多