2011-10-01 46 views
-1

這兩個代碼有什麼區別嗎?哪一個應該使用?瞭解asp.net 4.0中的「using」語句 - C#

asp.net 4.0,C#

代碼1:

using System; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.Data; 

public static class DbConnection 
{ 
    public static string srConnectionString = "server=localhost;database=mydb;uid=sa;pwd=mypw;"; 

    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 
     { 
      if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1) 
      { 
       using (SqlConnection connection = new SqlConnection(srConnectionString)) 
       { 
        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 
     { 
      strQuery = strQuery.Replace("'", "''"); 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       connection.Open(); 
       SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection); 
       command.ExecuteNonQuery(); 
      } 
     } 
    } 
} 

代碼2:

using System; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.Data; 

public static class DbConnection 
{ 
    public static string srConnectionString = "server=localhost;database=mydb;uid=sa;pwd=mypw;"; 

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

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

     catch 
     { 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1) 
       { 
        connection.Open(); 
        strQuery = strQuery.Replace("'", "''"); 

        using (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(); 
       using (SqlCommand command = new SqlCommand(strQuery, connection)) 
       { 
        command.ExecuteNonQuery(); 
       } 
      } 
     } 
     catch 
     { 
      strQuery = strQuery.Replace("'", "''"); 
      using (SqlConnection connection = new SqlConnection(srConnectionString)) 
      { 
       connection.Open(); 
       using (SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection)) 
       { 
        command.ExecuteNonQuery(); 
       } 
      } 
     } 
    } 
} 
+1

這兩個例子有什麼問題嗎?對不起,這些代碼示例太長,以至於無法區分它們的區別。 – millimoose

+0

看看裏面的try塊。哪一個更好或有什麼不同? – MonsterMMORPG

+0

'using'是一種語言關鍵字。它與ASP.NET –

回答

2

如果你有ReSharper的來爲你做它還是願意手動下載,您可以查看SqlCommandSqlDataAdapter的源代碼並查看它們的Dispose()方法是否實際執行任何操作。如果不是的話,可以放棄使用他們周圍的陳述。

+0

其實我很好奇。只有父母使用語句更好或嵌套使用語句。 – MonsterMMORPG

+1

你必須爲所有*對象使用''''來自動處理()'。在「using」塊中包裝函數不會自動處理該塊內的所有內容,而只會自動處理parens中的對象。 – millimoose

+0

所以它只處理()內的對象而不是{}對嗎? – MonsterMMORPG

1

你應該考慮的是,using子句用於確保在using子句的括號內使用的對象正確放置到內存中。

所以,我認爲你應該使用第二個代碼示例。