简体   繁体   English

Database.ExecuteSqlCommand()在执行存储过程时挂起

[英]Database.ExecuteSqlCommand() Hanging On Execution of Stored Procedure

I am working with a database that I do not have much control over. 我正在使用一个我没有太多控制权的数据库。 I am trying to execute a stored procedure using the Database.ExecuteSqlCommand method in a C# Web API call. 我正在尝试在C#Web API调用中使用Database.ExecuteSqlCommand方法执行存储过程。 I have been able to successfully use this method on other stored procedures with no issue. 我已经能够成功地在其他存储过程上使用此方法,而没有任何问题。 When I try to execute this particular stored procedure, however, the application freezes. 但是,当我尝试执行此特定的存储过程时,应用程序冻结。 I have it in a try catch and it doesn't throw any exceptions. 我将其放在try catch中,并且不会引发任何异常。 It will just stay in this state indefinitely. 它将无限期地保持这种状态。 I have to go into SSMS and kill the process associated to the call. 我必须进入SSMS并终止与呼叫相关的进程。 When I kill the process, the Web API continues. 当我终止该过程时,Web API将继续。 If I do the same EXEC command in SSMS, it works just fine returning a single row set of RETURN_VALUE = 0. I added this RETURN_VALUE = 0 manually at the end of the procedure thinking this might have been part of the issue. 如果我在SSMS中执行相同的EXEC命令,则返回单个行RETURN_VALUE = 0可以很好地工作。我在过程结束时手动添加了RETURN_VALUE = 0,以为这可能是问题的一部分。

The C# code is: C#代码为:

try
{
    entities.Database.ExecuteSqlCommand("EXEC [dbo].[SPCHANGEORDER]");
    return Ok();
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
    return Content(HttpStatusCode.BadRequest, "SQL Server Exception");
}

The stored procedure does call some other stored procedures as well as some inserts, updates, and deletes. 该存储过程确实会调用其他一些存储过程以及一些插入,更新和删除。 I know one of the nested stored procedures does several EXEC xp_cmdshell commands as well. 我知道其中一个嵌套存储过程也会执行几个EXEC xp_cmdshell命令。 I know this isn't a lot to go off of, but I hope someone can help me with a path to at least find the error. 我知道这还不算什么,但是我希望有人可以帮助我至少找到错误的路径。 Thanks! 谢谢!

EDIT I used SQL Server Profiler and found where it was being executed. 编辑我使用了SQL Server Profiler,并发现它在哪里执行。 I copy/pasted the sets into an SSMS query window, and was still successfully able to execute the stored proc. 我将集合复制/粘贴到SSMS查询窗口中,但仍然能够成功执行存储的proc。

Profiler Window 探查器窗口

探查器窗口

SSMS Query SSMS查询

SSMS查询

EF wraps native SQL Command calls in a transaction, by default. 默认情况下,EF将本机SQL Command调用包装在事务中。 Depending on what your stored procedure does, this might be the issue. 取决于您的存储过程做什么,这可能是问题所在。 For instance if your stored procedure makes linked server calls, this could cause it to require a Distributed Transaction. 例如,如果您的存储过程进行了链接服务器调用,则可能导致它需要分布式事务。

What I suspect is happening here is the transaction is causing an undetectable deadlock, since your procedure is calling xp_cmdshell. 我怀疑这里发生的事情是事务导致无法检测到的死锁,因为您的过程正在调用xp_cmdshell。 If that external program is trying to read data written by your stored procedure, wrapping it in a transaction would cause that external program to either not see your changes, or block on your uncommitted transaction. 如果该外部程序试图读取您的存储过程写入的数据,则将其包装在事务中将导致该外部程序看不到您的更改,或者阻止您未提交的事务。 (Since your stored proc is using xp_cmdshell to indirectly create a second connection, SQL Server can't detect the deadlock.) (由于您存储的proc使用xp_cmdshell间接创建第二个连接,因此SQL Server无法检测到死锁。)

Anyway to disable the transaction try: 无论如何要禁用事务,请尝试:

entities.Configuration.EnsureTransactionsForFunctionsAndCommands = false;
entities.Database.ExecuteSqlCommand("EXEC [dbo].[SPCHANGEORDER]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Database.ExecuteSqlCommand调用存储过程时,如何指定参数 - How can I specify parameters when calling a stored procedure with Database.ExecuteSqlCommand 如何从Database.ExecuteSqlCommand调用的存储过程中获取字符串结果 - How can I get a string result from a stored procedure called by Database.ExecuteSqlCommand Database.ExecuteSqlCommand始终返回-1 - Database.ExecuteSqlCommand always returns -1 实体框架:Database.ExecuteSqlCommand方法 - Entity Framework: Database.ExecuteSqlCommand Method Database.ExecuteSqlCommand-捕获SQL异常 - Database.ExecuteSqlCommand - capture SQL exceptions Database.ExecuteSqlCommand的返回值是多少? - What is the return value of Database.ExecuteSqlCommand? C# 覆盖实体框架 Database.ExecuteSqlCommand - C# override Entity Framework Database.ExecuteSqlCommand 在 mysql 上使用带参数的 Database.ExecuteSqlCommand 的正确语法是什么? - What is the correct syntax for using Database.ExecuteSqlCommand with parameters on mysql? 如何使用ExecuteSqlCommand从SQL存储过程获取值列表 - How to get list of values from SQL stored procedure using ExecuteSqlCommand 使用 ExecuteSqlCommand 调用存储过程(需要未提供的参数) - Call stored procedure using ExecuteSqlCommand (expects parameters which was not supplied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM