问题:

我在using语句中有一个IDbTransaction,但我不确定如果在using语句中抛出异常,它是否会被回滚。 我知道using语句会强制调用Dispose()……但是有人知道Rollback()是否也是如此?

更新:此外,我是否需要显式调用Commit(),如下所示,还是由using语句处理吗?

我的代码看起来像这样:

using Microsoft.Practices.EnterpriseLibrary.Data;

...

using(IDbConnection connection = DatabaseInstance.CreateConnection())
{
    connection.Open();

    using(IDbTransaction transaction = connection.BeginTransaction())
    {
       //Attempt to do stuff in the database
       //potentially throw an exception
       transaction.Commit();
    }
}

I’ve got an IDbTransaction in a using statement but I’m unsure if it will be rolled back if an exception is thrown in a using statement.I know that a using statement will enforce the calling of Dispose()…but does anyone know if the same is true for Rollback()?Update: Also, do I need to call Commit() explicitly as I have below or will that also be taken care of by the using statement?My code looks sort of like this:

问题:

我们有在SQL Server 2005上运行某些SQL的客户端应用程序,如下所示:

BEGIN TRAN;
INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
COMMIT TRAN;

它由一个长字符串命令发送。

如果插入操作之一失败,或者命令的任何部分失败,SQL Server会回滚事务吗? 如果不回滚,是否必须发送第二条命令来回滚?

我可以提供有关正在使用的api和语言的详细信息,但我认为SQL Server对任何语言都应做出相同的响应。

We have client app that is running some SQL on a SQL Server 2005 such as the following:It is sent by one long string command.If one of the inserts fail, or any part of the command fails, does SQL Server roll back the transaction?If it does not rollback, do I have to send a second command to roll it back?I can give specifics about the api and language I’m using, but I would think SQL Server should respond the same for any language.