2011-09-14 54 views
-2

這是我的通用數據庫連接類。我正在使用這個類來通過網站執行我的查詢。你會如何提高性能的建議。謝謝。可以改進此C#4.0 MSSQL 2008 R2數據庫連接類 - 專家問題

MSSQL 2008 R2 SP1 - 微軟的Visual Studio 2010 SP1,C#4.0 - ASP.net 4.0

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Linq; 
using System.Web; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.Data; 
using System.IO; 

/// <summary> 
/// Summary description for DbConnection 
/// </summary> 
public class DbConnection 
{ 
    public static string srConnectionString = "server=localhost;database=myDB;uid=sa;pwd=MYPW;"; 

    public DbConnection() 
    { 

    } 

    public static DataSet db_Select_Query(string strQuery) 
    { 
     DataSet dSet = new DataSet(); 

     try 
     { 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       connection.Open(); 
       SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection); 
       DA.Fill(dSet); 
      } 
      return dSet; 
     } 

     catch (Exception) 
     { 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1) 
       { 
        connection.Open(); 
        strQuery = strQuery.Replace("'", "''"); 
        SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection); 
        command.ExecuteNonQuery(); 
       } 
      } 
      return dSet; 
     } 
    } 

    public static void db_Update_Delete_Query(string strQuery) 
    { 
     try 
     { 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       connection.Open(); 
       SqlCommand command = new SqlCommand(strQuery, connection); 
       command.ExecuteNonQuery(); 
      } 
     } 
     catch (Exception) 
     { 
      strQuery = strQuery.Replace("'", "''"); 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       connection.Open(); 
       SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection); 
       command.ExecuteNonQuery(); 
      } 

     } 
    } 
} 
+0

1)您在調整性能之前是否確定了瓶頸? – Andrey

+1

2)永遠不會在'catch(Exception)'中寫入邏輯,應該有錯誤處理代碼 – Andrey

+1

此外,不要登錄到數據庫。如果數據庫因交易/連接而變得不堪重負,那麼增加這種負擔就毫無意義。 – WiseGuyEh

回答

1

1)你是如何確保在strQuery在不進行傳遞到SQL注入?

2.)使用日誌框架,如nlog或log4net。這將允許您通過使用配置文件輕鬆指定將錯誤日誌(文件,電子郵件,數據庫)存儲在哪裏。

你的日誌會是這樣,而不是:

try 
{ 
    using (SqlConnection connection = new SqlConnection(srConnectionString)) 
    { 
     connection.Open(); 
     SqlCommand command = new SqlCommand(strQuery, connection); 
     command.ExecuteNonQuery(); 
    } 
} 
catch (Exception ex) 
{ 
    log.ErrorFormat("strQry: {0}", strQuery); 
    log.Error(ex); 
} 

3)使用SecureString的

public static SecureString srConnectionString = "server=localhost;database=myDB;uid=sa;pwd=MYPW;"; 

4)您是如何打算的錯誤寫入數據庫,如果數據庫是下?它會產生未捕獲的異常...

+0

所有使用這個類所做的查詢是由我寫的而不是用戶生成的,因此它們都是安全的到sql注入。我正在使用用戶生成的查詢不同的方法。數據庫永遠不會倒下這是我想的:D這個日誌存儲在哪裏? – MonsterMMORPG

+0

@MonsterMMORPG你可以在配置文件中指定日誌的位置。它可能是一個文件目錄或電子郵件或存儲在數據庫中(它甚至可能是一個不同於你的應用程序的數據庫)。 – clyc