0

我一直在使用数据表来显示我的网站中 SQL 2005 数据库表中的一些字段,该网站是在 ASP.NET 2.0 中构建的。

最近它一直在抛出超时错误,说已经达到所有最大连接池。

这是我使用 SqlDataAdapter 在Repeater OnDataItemBound 上执行此操作的代码。

#region repeater item databound
    protected void rProducts_OnItemDataBound(object source, RepeaterItemEventArgs e)
    {

        // displaying sale and original price //
        Label lblitemid = e.Item.FindControl("itemID") as Label;
        decimal strOP;
        decimal strSP;
        string salequery = "select SaleItemNo, OriginalPrice, SalePrice, OriginalPriceRange, SalePriceRange from SaleItems where CatalogItemId='" + lblitemid.Text + "'";
        SqlConnection myconnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString);
        SqlCommand SqlCmd1 = null;
        SqlCmd1 = new SqlCommand(salequery, myconnection1);
        SqlDataAdapter ad1 = new SqlDataAdapter(SqlCmd1);
        DataTable dt1 = new DataTable();
        ad1.Fill(dt1);

        Label lbloriprice = e.Item.FindControl("originalprice") as Label;
        if (dt1.Rows[0]["OriginalPrice"].ToString() == "" || dt1.Rows[0]["OriginalPrice"].ToString() == "0.00")
        {
            lbloriprice.Attributes.Add("style", "display:none");
        }
        else
        {
            strOP = Convert.ToDecimal(dt1.Rows[0]["OriginalPrice"].ToString());
            lbloriprice.Text = strOP.ToString("c");
        }

        Label lbloprange = e.Item.FindControl("opRange") as Label;
        if (dt1.Rows[0]["OriginalPriceRange"].ToString() != "")
        {
            lbloprange.Text = dt1.Rows[0]["OriginalPriceRange"].ToString();
            lbloprange.Visible = true;
            lbloriprice.Visible = false;
        }

        Label lblsaleprice = e.Item.FindControl("saleprice") as Label;
        if (dt1.Rows[0]["SalePrice"].ToString() == "" || dt1.Rows[0]["SalePrice"].ToString() == "0.00")
        {
            lblsaleprice.Attributes.Add("style", "display:none");
        }
        else if (dt1.Rows[0]["SalePriceRange"].ToString() != "")
        {
            lblsaleprice.Text = "Special <br />" + dt1.Rows[0]["SalePriceRange"].ToString();
        }
        else
        {
            strSP = Convert.ToDecimal(dt1.Rows[0]["SalePrice"].ToString());
            lblsaleprice.Text = "Special <br />" + strSP.ToString("c");
        }

    }

    #endregion

它在 ad1.fill(dt1) 上抛出错误

我重新设计了我的代码,想知道这是否会减少或消除错误。请指教

   #region repeater item databound
    protected void rProducts_OnItemDataBound(object source, RepeaterItemEventArgs e)
    {

        // displaying sale and original price //
        Label lblitemid = e.Item.FindControl("itemID") as Label;

        SqlDataSource SqlDataSource2 = new SqlDataSource();
        SqlDataSource2.ID = "SqlDataSource2";
        SqlDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString;
        SqlDataSource2.SelectCommand = "select SaleItemNo, OriginalPrice, SalePrice, OriginalPriceRange, SalePriceRange from SaleItems where CatalogItemId='" + lblitemid.Text + "'";

        Repeater rep = e.Item.FindControl("rpt2") as Repeater;
        rep.DataSource = SqlDataSource2;
        rep.DataBind();

     }
#endregion
4

1 回答 1

1

SqlCommandSqlConnection, 和SqlDataAdapter都需要在using块中。您的连接可能真的用完了,因为您没有及时关闭它们。


块示例using

using (SqlConnection myconnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString))
{
    using (SqlCommand sqlCmd1 = new SqlCommand(salequery, myconnection1))
    {
        using (SqlDataAdapter ad1 = new SqlDataAdapter(sqlCmd1))
        {
            dt1 = new DataTable();
            ad1.Fill(dt1);
        }
    }
}
于 2012-10-26T02:27:08.503 回答