代码不断超时
因此,我们得到的这组代码由于某种原因一直超时。它运行的不是存储过程,因为它运行良好。另外,如果我们从 C# 代码中删除该参数,代码就会运行。该参数不断中断(导致超时),我们无法找出原因。
C#:
public static PTWViewList GetList(int studynumber)
{
PTWViewList tempList = new PTWViewList();
using (SqlConnection myConnection = new SqlConnection(AppConfiguration.cnARDB))
{
string spName = "ardb.PTWViewSelect";
SqlCommand myCommand = new SqlCommand(spName, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@study", studynumber);
myConnection.Open();
using (NullableDataReader myReader = new NullableDataReader(myCommand.ExecuteReader())) /*this is where the code times out*/
{
tempList = new PTWViewList();
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
myReader.Close();
}
}
tempList.ListCount = tempList.Count;
return tempList;
}
存储过程:
CREATE PROCEDURE [ardb].[PTWViewSelect]
@studynumber int = NULL,
@quoteid uniqueidentifier = NULL,
@lineitemid uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT
[Study]
,[LineItemID]
,[QuoteID]
,[Total]
,[COOP]
,[VendorCost]
,[CustCost]
,[LineItemNumber]
,[StudyTypeCode]
,[GroupLeader]
,[PTWDate]
,[PONumber]
,[POStatus]
,[StudyDirector]
,[SL_DESC_L]
,[SL_Code]
,ProjectDescription
,CreatedBy
,chARProcess
,CODate
FROM
[ARDB].[dbo].[PTWView]
WHERE
(@studynumber is null or StudyNumber=@studynumber)
AND (@quoteid is null or QuoteID=@quoteid)
AND (@lineitemid is null or LineItemID = @lineitemid)
END
So, we've got this set of code that, for some reason, keeps timing out. It's not the stored procedure that it's running, because that runs fine. Also, if we remove the parameter from the c# code, the code runs. The parameter keeps breaking (causing it to time out) and we can't figure out why.
c#:
public static PTWViewList GetList(int studynumber)
{
PTWViewList tempList = new PTWViewList();
using (SqlConnection myConnection = new SqlConnection(AppConfiguration.cnARDB))
{
string spName = "ardb.PTWViewSelect";
SqlCommand myCommand = new SqlCommand(spName, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@study", studynumber);
myConnection.Open();
using (NullableDataReader myReader = new NullableDataReader(myCommand.ExecuteReader())) /*this is where the code times out*/
{
tempList = new PTWViewList();
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
myReader.Close();
}
}
tempList.ListCount = tempList.Count;
return tempList;
}
stored procedure:
CREATE PROCEDURE [ardb].[PTWViewSelect]
@studynumber int = NULL,
@quoteid uniqueidentifier = NULL,
@lineitemid uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT
[Study]
,[LineItemID]
,[QuoteID]
,[Total]
,[COOP]
,[VendorCost]
,[CustCost]
,[LineItemNumber]
,[StudyTypeCode]
,[GroupLeader]
,[PTWDate]
,[PONumber]
,[POStatus]
,[StudyDirector]
,[SL_DESC_L]
,[SL_Code]
,ProjectDescription
,CreatedBy
,chARProcess
,CODate
FROM
[ARDB].[dbo].[PTWView]
WHERE
(@studynumber is null or StudyNumber=@studynumber)
AND (@quoteid is null or QuoteID=@quoteid)
AND (@lineitemid is null or LineItemID = @lineitemid)
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有没有尝试过
而不是:
have you tried
instead of:
编辑
如果传递参数是问题所在,那么问题就归结为存储过程执行所需的时间。 SQL Server 的默认超时通常为 120 秒。您可以添加“连接超时”以增加数据库连接字符串中的超时并签出。
** 旧答案 - 忽略 **
如果没有堆栈跟踪,并且相信您所说的存储过程很好,我猜测它由于连接失败而超时。该代码无法连接到您的数据库服务器,因此超时。
EDIT
If passing parameters is the problem, then it comes down to how much time the stored procedure takes to execute. Default timeout for SQL server is usually 120 secs. You can add "Connect Timeout" to increase timeout in your DB connection string and check out.
** Old Answer -- Ignore **
Without stack trace, and taking your word that the stored procedure is fine, I am guessing that it is timing out due to the connection failure. The code is unable to connect to your DB server and hence timing out.
将 arithabort 设置为关闭使 sp 需要 45 秒,而不是 1。将其重新设置为 1。我更新了存储过程以将其设置为打开,应用程序中没有任何更改。改成关闭了,没啥变化。然后我删除了更新,然后应用程序运行正常。
我相信发生的事情是更新存储过程导致它重新编译,从而解决了问题。不过我对此并不是 100% 确定。
setting arithabort off made the sp take 45 seconds as opposed to 1. setting it back on changed it back to 1. I updated the stored procedure to set it on, no change in the app. Changed it to off, no change. I then removed the update and then the app worked fine.
I believe what happened is that updating the stored procedure caused it to recompile, fixing the issue. I'm not 100% sure on this though.
一件事可能是 ARITHABORT 设置,将其设置为 ON...NET 默认为 OFF
在 SSMS 中运行进程,将 ARITHABORT 设置为 OFF,看看它现在是否运行得更慢,就像 .NET
示例
中的那样另一件事是您的 WHERE 子句不是最佳的,请查看 您在 WHERE 子句中使用 Column=@Param OR @Param IS NULL 吗?不,它不执行
使用SSMS中的参数,过程是否运行缓慢?可以展示一下执行计划吗?
One thing could be the
ARITHABORT
setting, set it to ON...NET defaults to OFFrun the proc in SSMS with ARITHABORT set to OFF and see if it runs slower now like from .NET
example
Another thing is that your WHERE clause is not optimal, take a look at Do you use Column=@Param OR @Param IS NULL in your WHERE clause? Don't, it doesn't perform
does the proc run slow with parameters in SSMS? Can you show the execution plan?