为什么我的ASP.NET项目中出现此错误?

问题描述:

从池中获取连接之前经过的超时时间。这可能是因为所有池连接都在使用中并且达到了最大池大小。



The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class UserHome : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=.\\sql2008;initial catalog=ProductivePlusDB;user id=sa;password=123456");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["emailid"] == null || Session["emailid"].ToString() == "")
        {
            Response.Redirect("Default.aspx");
        }
        
       // string user = System.Environment.UserName;
       // Response.Write(user);

        try
        {

            con.Open();
            string qry = "select * from ShopOwnMast where EmailId='" + Session["emailid"].ToString() + "'";
            SqlDataAdapter adp = new SqlDataAdapter(qry, con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            Session["ShopId"] = dt.Rows[0][8].ToString();


            string shopid = Session["ShopId"].ToString();
            string qry1 = "Select * from ShopMast where shopid ='" + shopid + "'";
            SqlDataAdapter adp1 = new SqlDataAdapter(qry1, con);
            DataTable dt1 = new DataTable();
            adp1.Fill(dt1);
            lblShopName.Text = dt1.Rows[0][1].ToString();
            getdata();

        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex);
        }
        finally 
        {
            con.Close();
        }

        
        //Response.Write(Session["ShopId"].ToString());

    }





我是什么尝试过:



i不知道该怎么做。我的项目现在没有运行..





我在关闭和打开连接时犯了错误吗?

我该怎么办?



What I have tried:

i have no idea what to do. my project is not running now..


did i made mistake in closing and opening connection?
what should i do?

您转贴的问题会显示更多信息 - 请勿这样做,使用改善问题编辑现有问题以提供更好的信息 - 并且它可能显示问题所在。

Your reposted question shows a little more info - don't do that, edit your existing question using the Improve Question to provide better info - and it's possible that that shows what the problem is.
try
        {
 
            con.Open();
            string qry = "select * from ShopOwnMast where EmailId='" + Session["emailid"].ToString() + "'";
            SqlDataAdapter adp = new SqlDataAdapter(qry, con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            Session["ShopId"] = dt.Rows[0][8].ToString();
 

            string shopid = Session["ShopId"].ToString();
            string qry1 = "Select * from ShopMast where shopid ='" + shopid + "'";
            SqlDataAdapter adp1 = new SqlDataAdapter(qry1, con);
            DataTable dt1 = new DataTable();
            adp1.Fill(dt1);
            lblShopName.Text = dt1.Rows[0][1].ToString();
            getdata();
 
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex);
        }
        finally 
        {
            con.Close();
        }

要检查的事情:

首先,如果你只想使用一行和一列,不要使用SELECT * FROM - 它是非常低效。而是使用

Couple of things to check:
First off, don't use SELECT * FROM if you are only going to use one row and one column - it's very inefficient. Instead use

SELECT TOP 1 MYColumnName FROM

TOP 1 part将它限制为一行(这是你希望得到的行)。

命名列意味着你不会获取你不会使用的信息。

让我担心的第二件事是,当你使用了一个数据项时,你调用一个名为 getdata 的方法 - 我想知道那个方法是不是你所在的人。即

The TOP 1 part restricts it to one row (which is what you would hopefully get anyway).
The named column means you don't fetch information you aren't going to use.
The second thing that worries me is that when you have used the one data item, you call a method called getdata - and I'm wondering if that method is the one you are in. I.e.

private void getdata()
{
    try
    {

        con.Open();
        string qry = "select * from ShopOwnMast where EmailId='" + Session["emailid"].ToString() + "'";
        SqlDataAdapter adp = new SqlDataAdapter(qry, con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        Session["ShopId"] = dt.Rows[0][8].ToString();


        string shopid = Session["ShopId"].ToString();
        string qry1 = "Select * from ShopMast where shopid ='" + shopid + "'";
        SqlDataAdapter adp1 = new SqlDataAdapter(qry1, con);
        DataTable dt1 = new DataTable();
        adp1.Fill(dt1);
        lblShopName.Text = dt1.Rows[0][1].ToString();
        getdata();

    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex);
    }
    finally
    {
        con.Close();
    }
}

如果是这样,那么你的代码将以递归方式调用自身,并且将非常非常快速地使用整个SQL连接池 - 这是其中之一可能很容易导致你的效果的东西。



正如理查德所说:不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

If so, then your code will recursively call itself, and that will use the entire SQL connection pool very, very quickly - which is one of the things that could easily cause the effect you have.

And as Richard has said: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


我不会评论您的样式或我在代码中看到的其他不良内容。我会假设你只是想让一些东西工作。



我的建议是尝试使用连接块和sqldataadapter来确保这些对象被处理掉所有连接都关闭。



I won't comment on your style or other bad things I see in your code. I will assume you are just trying to get something working.

My suggestion would be to try a using block for the connection and sqldataadapter which will ensure that these objects get disposed and all connections get closed.

using (con = new SqlConnection("DataSource=.\\sql2008;initialcatalog=ProductivePlusDB;user id=sa;password=123456")
{
    con.Open();
    string qry = "select * from ShopOwnMast where EmailId='" +  
        Session["emailid"].ToString() + "'";

    using (SqlDataAdapter adp = new SqlDataAdapter(qry, con))
    {
        // all your other code
    }
}


使用它将确保您的连接已关闭。



我不确定它是否使用了池化连接。您通常需要根据我的理解将池参数放在连接字符串中。
Using it will ensure that your connections are closed.

I'm not sure its even using a pooled connection. You normally need to put pool parameters in your connection string from what I understand.