我确定这是预期的行为,我只是想更好地理解它。我发现在以下情况下将事务设置为null:
没关系。对我来说似乎很奇怪的是,如果我打开ANSI_WARNINGS(删除上面的第一个条件),则事务不会设置为null。
为什么?让我知道是否有一些我错过的ANSI_WARNINGS文档可以解释这一点。
重现问题的代码:
using System.Data.SqlClient;
using System;
public class Program
{
//CREATE TABLE Junk( Name varchar(12) )
//DENY INSERT ON Junk to someUser
public static void Main(string[] args)
{
bool ansi_warnings_off = false;
int result;
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection("Data Source=dbServerName;Initial Catalog=dbName;Persist Security Info=True;User ID=someUser;Password=somePassword;"))
{
conn.Open();
cmd.Connection = conn;
using (var trans = conn.BeginTransaction())
{
cmd.Transaction = trans;
if (ansi_warnings_off)
{
cmd.CommandText = "SET ANSI_WARNINGS OFF";
result = cmd.ExecuteNonQuery();
Console.WriteLine("===Result of setting ansi_warnings off = " + result);
}
try
{
cmd.CommandText = "INSERT INTO Junk(Name) VALUES ('test')";
result = cmd.ExecuteNonQuery();
Console.WriteLine("===Result of insert = " + result);
}
catch (Exception e)
{
Console.WriteLine("===Exception for insert = " + e.Message);
}
Console.WriteLine("===Transaction is null? " + (cmd.Transaction == null));
}
conn.Close();
}//using
}//main
}
ansi_warnings_off = true时的输出:
===Result of setting ansi_warnings off = -1
===Exception for insert = The INSERT permission was denied on the object 'Junk', database 'dbName', schema 'dbo'.
===Transaction is null? True
ansi_warnings_off = false时的输出:
===Exception for insert = The INSERT permission was denied on the object 'Junk', database 'dbName', schema 'dbo'.
===Transaction is null? False
编辑以简化,如果您只是想在SQL Server Management Studio中运行它,这里也会出现一些重现问题的T-SQL。
--------do this as sa---------
CREATE TABLE Junk( Name varchar(12) )
INSERT INTO Junk(Name) VALUES ('original')
DENY INSERT ON Junk to dbUser
---------------------------------
--now do the next two as dbUser
-----update is not attempted----
SET ANSI_WARNINGS OFF
BEGIN TRAN
INSERT INTO Junk(Name) VALUES ('new')
update Junk SET Name = 'updated' where Name = 'original'
COMMIT TRAN
--------------------------------
--------update is done------
SET ANSI_WARNINGS ON
BEGIN TRAN
INSERT INTO Junk(Name) VALUES ('new')
update Junk SET Name = 'updated' where Name = 'original'
COMMIT TRAN
-----------------------------