0

我有 C# 代码 (wcf) 连接到数据库并插入一个完整的“报告”,包括地址表、人员表等,所有这些都必须通过调用存储过程来完成。附带说明,某些存储过程可能会被多次调用(因为我们可能在报告中有多个人和多个地址等)

插入地址(通过调用 spInsertAddress) 插入人员(通过调用 spInsertPerson) 插入项目(调用另一个存储过程)引发错误(可能是字符太多,或者为日期/时间字段插入错误值)

如何撤消存储过程所做的所有更改,因为它们已经进行了?

在 MSDN 上找到这篇文章,解释如何通过 C# 添加事务并使用 try/catch 提交或回滚事务

4

2 回答 2

1

您可以从 C# (MSDN)调用 BeginTransaction ,然后根据需要提交或回滚事务。但是您必须确保存储过程本身不会执行 COMMIT 或 ROLLBACK 语句。请注意,Sql Server 不支持您可能期望的嵌套事务。

于 2013-05-22T23:40:03.200 回答
0

From my experience, after a transaction commits there is no rollback. I'd suggest you rewrite the code that calls the inserts ensuring you manage the values sent so errors are not an issue. It is best practice to validate all data prior to saving to the DB. You didn't mention the DB in use, some DB have different options available.

于 2013-05-22T23:02:20.357 回答