2012-10-25 30 views
0

我一直在使用數據表顯示SQL 2005數據庫表中的一些字段,這些字段是在ASP.NET 2.0中構建的網站中的。以下哪個代碼在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 
+0

會發生什麼事,當你嘗試新的代碼?沒有人可以猜測這個代碼是否會有所幫助,因爲很難猜測性能問題在哪裏。 –

+0

您應該查看使用參數,而不是直接將用戶輸入附加到您的選擇中。它讓您打開SQL注入攻擊。 –

回答

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); 
     } 
    } 
} 
+0

您好約翰我認爲SqlDataAdapeter會自動打開和關閉,請給我一個在塊中使用它們的例子。欣賞它 – niceoneishere

+0

嗨,John感謝您的回覆,在上面的示例中應該打開和關閉連接。欣賞它 – niceoneishere

+0

我認爲'SqlDataAdapter'會爲你做到這一點,否則,在'Fill'之前會很好。 –