ADO.Net的交易连接到SQL Server 2000和SQL Server 2008的差异
问题描述:
我有一个与SQL Server 2000和SQL Server 2008ADO.Net的交易连接到SQL Server 2000和SQL Server 2008的差异
SqlConnection con = new SqlConnection("connecstionstring");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MySp";
con.Open();
SqlTransaction trans = con.BeginTransaction();
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
trans.Commit();// this step will fail with sql server 2008 but success with 2000
con.Close();
和SP代码的作用不同一些C#/ ado.net代码如:
ALTER PROCEDURE MySp
AS
BEGIN
COMMIT --this match the outside 'begin trans' in c# code
do some thing...
BEGIN TRANSACTION -- this match the outside 'commit' in c# code
END
当连接到SQL Server 2008,C#代码将无法在 'trans.Commit()',和异常说:交易已经COMMITED,不能再使用。
但连接到SQL Server 2000,它会成功。请告诉我为什么?
答
我不确定,但我想sp提交sql server 2008中的事务,并且它不能被提交两次。