从C#调用事务时出现问题 [英] Problem calling a transaction from C#
问题描述
大家好!
我在SQL服务器中有一个名为"""的交易(如下所示)
电话是:
dbCommand.ExecuteNonQuery();
看起来上面的代码不起作用,因为交易本身有效,所以<$ p>
-------- ----------------------------------------------
创建
PROCEDURE [dbo] 。 [deleteCarOwnersWithManyCars]
( @ Pe_Id
int ,
@numberOfCars
int )
AS
BEGIN
TRAN deleteCarOwnersWithManyCars
SELECT
@numberOfCars
= COUNT (*) FROM Tbl_Bil
WHERE Bi_Agare
= @ Pe_Id
DELETE
FROM Tbl_Bil
WHERE Bi_Agare
= @ Pe_Id
DELETE
FROM Tbl_Person
WHERE Pe_Id
= @ Pe_Id
IF
@ numberOfCars
< 2
  ;
BEGIN
ROLLBACK TRAN deleteCarOwnersWithManyCars
PRINT 'Har
endast en bil'
END
ELSE
BEGIN
COMMIT TRAN deleteCarOwnersWithManyCars
PRINT 'Har
flera bilar'
END
------------------- -----------------------------------
可能是什么问题 通话: dbCommand.ExecuteNonQuery();
希望你能帮助我: - (
嗨Mats_Nystrom,
根据您的描述和相关代码,我创建了一个简单的使用SqlCommand的演示,它运行良好。你能不能提供更多的信息,比如相关的完整代码,详细的错误信息。
string connstr = @" Data Source =(localdb)\ MSSQLLocalDB; Initial Catalog = AdoDotNetDemo; Integrated Security = True" ;;
using(var cn = new SqlConnection(connstr))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =" deleteCarOwnersWithManyCars" ;;
cmd.Connection = cn;
cmd.Parameters.AddWithValue(" @ Pe_Id",1);
cmd.Parameters.AddWithValue(" @ numberOfCars",0);
cn.Open();
cmd.ExecuteNonQuery();
Console.WriteLine(" OK");
Console.ReadKey();
}祝你好运,
Cole Wu
Hello everybody!
I've a transaction in SQL server called ""(shown below)
The call is :
dbCommand.ExecuteNonQuery();
It seems like this code above does not work,
the transaction itself works:
------------------------------------------------------
CREATE
PROCEDURE[dbo].[deleteCarOwnersWithManyCars]
(@Pe_Id
int,
@numberOfCars
int)
AS
BEGIN
TRANdeleteCarOwnersWithManyCars
SELECT
@numberOfCars =COUNT(*)FROMTbl_Bil WHEREBi_Agare =@Pe_Id
DELETE
FROMTbl_Bil WHEREBi_Agare =@Pe_Id
DELETE
FROMTbl_Person WHEREPe_Id =@Pe_Id
IF
@numberOfCars <2
BEGIN
ROLLBACKTRANdeleteCarOwnersWithManyCars
PRINT'Har endast en bil'
END
ELSE
BEGIN
COMMITTRANdeleteCarOwnersWithManyCars
PRINT'Har flera bilar'
END
------------------------------------------------------
What can be the problem with the call: dbCommand.ExecuteNonQuery();
Hope you can help me :-(
Hi Mats_Nystrom,
Based on your description and related code, I create a simple demo by using SqlCommand, it works well. Could you please provide a bit more information, such as related complete code, detailed error message.
string connstr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=AdoDotNetDemo;Integrated Security=True"; using (var cn = new SqlConnection(connstr)) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "deleteCarOwnersWithManyCars"; cmd.Connection = cn; cmd.Parameters.AddWithValue("@Pe_Id", 1); cmd.Parameters.AddWithValue("@numberOfCars", 0); cn.Open(); cmd.ExecuteNonQuery(); Console.WriteLine("OK"); Console.ReadKey(); }Best regards,
Cole Wu
这篇关于从C#调用事务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!