超时已过。从池中获取连接之前超时时间已过

发布于 2024-11-27 01:35:59 字数 2066 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

养猫人 2024-12-04 01:36:00

也许问题与 SqlDataReader 对象有关,该对象未关闭。尝试使用 using 块:

using (var Reader = Command.ExecuteReader())
{

作为旁注,如果未找到任何行,Read 函数将返回 false。所以你可以将其缩短

Reader.Read();
if (Reader.HasRows)
{

为:

if (Reader.Read())
{

Perhaps the problem is related to the SqlDataReader object, which is not closed. Try a using block:

using (var Reader = Command.ExecuteReader())
{

As a sidenote, the Read function returns false if no rows were found. So you could shorten this:

Reader.Read();
if (Reader.HasRows)
{

to:

if (Reader.Read())
{
莫多说 2024-12-04 01:36:00

不,当存在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文