以下代码中的哪一个在ASP.NET 2.0中具有更好的性能

时间:2012-10-25 20:46:50

标签: c# sql-server-2005 asp.net-2.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

1 个答案:

答案 0 :(得分:1)

您的SqlCommandSqlConnectionSqlDataAdapter都必须位于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);
        }
    }
}