我有一个删除行的存储过程。示例代码:
CREATE PROCEDURE [dbo].[ap_um_delete_tems_grid]
(
@agency char(3)
, @subagency char(1)
, @grid_id int
, @role_id int
)
AS
DELETE FROM [grid_setup]
WHERE agency = @agency
AND subagency = @subagency
AND gs.grid_id = @grid_id
AND role_id = @role_id
如果我直接从 Sql Mgmt Studio 执行这个 SProc,它就可以正常工作。但是,如果我尝试使用事务在 C# 代码中执行此操作,我会得到一个非常有趣的 SqlException:
'ap_um_delete_tems_grid' 附近的语法不正确
异常详细信息说它发生在第 1 行。C# 代码是:
using (SqlConnection connection = new SqlConnection(conn.TemsConnectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start the local transaction
transaction = connection.BeginTransaction("GridReplaceTransaction");
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText = "ap_um_delete_tems_grid";
SqlParameter parm = new SqlParameter();
// Adding the parameters
int cnt = command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
上面的代码在我将其放入事务之前有效。为什么我收到语法错误?也许是因为在 SProc 中需要一些东西来支持事务?还是我搞砸了别的东西?
注意:请不要试图告诉我我不需要单次删除的事务。以上是多步骤过程的一部分,即在删除后添加新行。谢谢。