超时已过。从池中获取连接之前超时时间已过
我在 ASP.NET 开发方面相当新,所以你可以说我开发了一个编码很差的应用程序,导致了我这个错误。
超时已过。从池中获取连接之前超时时间已过。
我尝试在 google 上搜索,发现这是由于应用程序池中未关闭的连接而发生的,因此我仔细检查了我的应用程序,并为所有 Sqlconnections 插入了 using 关键字,但仍然如此我遇到了这个问题,这是我的应用程序中的一些示例代码。所有 Sqlconnection 都包含在这样的 using 语句中。
using (Connection = new SqlConnection(CIPConnection))
{
string ReturnValue = String.Empty;
try
{
SqlDataReader Reader;
Connection.Open();
string CommandText = "SELECT * FROM Users WHERE NAME = @NAME AND PASSWORD = @PASSWORD";
Command = new SqlCommand(CommandText, Connection);
Command.Parameters.AddWithValue("@NAME", UserName);
Command.Parameters.AddWithValue("@PASSWORD", Password);
Reader = Command.ExecuteReader();
Reader.Read();
if (Reader.HasRows)
{
Session["ID"] = Reader["ID"];
Session["NAME"] = Reader["NAME"];
Session["DEPARTMENT"] = Reader["DEPARTMENT"];
switch (Reader["DEPARTMENT"].ToString())
{
case "Admin":
ReturnValue = "Admin";
break;
case "Editing":
ReturnValue = "Editing";
break;
case "Sales and Support":
ReturnValue = "Sales and Support";
break;
case "Writing":
ReturnValue = "Writing";
break;
default:
ReturnValue = "Sorry";
break;
}
}
}
catch (Exception exp)
{
Response.Write(exp.Message.ToString());
}
return ReturnValue;
}
现在我的问题是即使在 using 语句块中我是否也需要关闭连接?关闭连接的最佳方式是什么? (将它放在finally块中?与每个try语句一起使用?)我还应该将using语句与SqlReader和SqlCommand一起使用吗?请告诉我摆脱未使用的连接的最佳方法,以便我可以解决这个问题。
谢谢。
I am fairly new in asp.net development, so you can say that I have developed a poorly coded application that is giving me this error.
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
I tried searching on google and found that it happens because of unclosed connections in the application pool so I carefully examined my application and inserted using keyword for all Sqlconnections but still I am having this problem, here is some sample code from my application. All Sqlconnections are enclosed in using statement like this.
using (Connection = new SqlConnection(CIPConnection))
{
string ReturnValue = String.Empty;
try
{
SqlDataReader Reader;
Connection.Open();
string CommandText = "SELECT * FROM Users WHERE NAME = @NAME AND PASSWORD = @PASSWORD";
Command = new SqlCommand(CommandText, Connection);
Command.Parameters.AddWithValue("@NAME", UserName);
Command.Parameters.AddWithValue("@PASSWORD", Password);
Reader = Command.ExecuteReader();
Reader.Read();
if (Reader.HasRows)
{
Session["ID"] = Reader["ID"];
Session["NAME"] = Reader["NAME"];
Session["DEPARTMENT"] = Reader["DEPARTMENT"];
switch (Reader["DEPARTMENT"].ToString())
{
case "Admin":
ReturnValue = "Admin";
break;
case "Editing":
ReturnValue = "Editing";
break;
case "Sales and Support":
ReturnValue = "Sales and Support";
break;
case "Writing":
ReturnValue = "Writing";
break;
default:
ReturnValue = "Sorry";
break;
}
}
}
catch (Exception exp)
{
Response.Write(exp.Message.ToString());
}
return ReturnValue;
}
Now my question is do I need to close Connection even in using statement block ? what will be the best way to close connection ? (putting it in finally block ? with every try statement ?) should I also use using statement with SqlReader and SqlCommand ? Please tell me the best way to get rid of unused connections so I can solve this problem.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许问题与
SqlDataReader
对象有关,该对象未关闭。尝试使用using
块:作为旁注,如果未找到任何行,
Read
函数将返回 false。所以你可以将其缩短为:
Perhaps the problem is related to the
SqlDataReader
object, which is not closed. Try ausing
block:As a sidenote, the
Read
function returns false if no rows were found. So you could shorten this:to:
不,当存在using块时,它会Dispose,Dispose会Close并将其返回到池中。问题出在其他地方,或者你漏掉了一个。
是否结束使用关闭打开的 SQL 连接
No, when it exists the using block, it will Dispose, which will Close and return it to the pool. The problem lies elsewhere, or you missed one.
Does End Using close an open SQL Connection