0

私はデータテーブルを使用して、ASP.NET2.0で構築されたWebサイトのSQL2005データベーステーブルのいくつかのフィールドを表示しています。

最近、すべての最大接続プールに達したというタイムアウトエラーがスローされています。

これは、リピーターOnDataItemBoundでこれを実行するSqlDataAdapterを使用する私のコードです。

#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

、、およびすべてSqlCommandをブロックにする必要があります。タイムリーに接続を閉じていないため、実際に接続が不足している可能性があります。SqlConnectionSqlDataAdapterusing


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 に答える