2010-04-29 23 views
0

我想知道在.Net中開發CRUD操作(特別是當您使用數據庫作爲數據源時)是否會有任何可信的方法用於try-catch塊的catch部分?.Net:在開發CRUD操作時,您在try-catch塊的「Catch」部分中有什麼自信的方法?

好吧,您對下面的行有什麼看法?

public int Insert(string name, Int32 employeeID, string createDate) 
    { 
     SqlConnection connection = new SqlConnection(); 
     connection.ConnectionString = this._ConnectionString; 
     try 
     { 
      SqlCommand command = connection.CreateCommand(); 
      command.CommandType = CommandType.StoredProcedure; 
      command.CommandText = "UnitInsert"; 
      if (connection.State != ConnectionState.Open) 
       connection.Open(); 
      SqlCommandBuilder.DeriveParameters(command); 
      command.Parameters["@Name"].Value = name; 
      command.Parameters["@EmployeeID"].Value = employeeID; 
      command.Parameters["@CreateDate"].Value = createDate; 
      int i = command.ExecuteNonQuery(); 
      command.Dispose(); 
      return i; 
     } 
     catch 
     { 
      **// how do you "catch" any possible error here?** 
      return 0; 
      // 
     } 
     finally 
     { 
      connection.Close(); 
      connection.Dispose(); 
      connection = null; 
     } 
    } 
+0

完全在一條切線上,但你應該嘗試使用'using'語句,而不是'try' /'finally'並自己調用'Dispose'。 http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx – 2010-04-29 12:02:42

+0

爲什麼要檢查連接狀態是否打開?它不會打開,所以只需調用'Open'即可。 – 2010-04-29 12:03:59

+0

@Matthew:順便說一句,這是什麼意思:完全切線? – odiseh 2010-04-29 12:37:17

回答

3

我會爲初學者使用using語句。

我不會返回0作爲失敗。您可以成功更新任何記錄,因此0將是有效的成功響應代碼。使用-1清楚地表明出了點問題。就個人而言,如果發生意外,我寧願拋出異常。

try 
    { 
     using (SqlConnection connection = new SqlConnection()) 
     { 

      connection.Open();   

      using(SqlCommand command = connection.CreateCommand()) 
      { 
        command.CommandType = CommandType.StoredProcedure; 
        command.CommandText = "UnitInsert"; 

        SqlCommandBuilder.DeriveParameters(command); 
        command.Parameters["@Name"].Value = name; 
        command.Parameters["@EmployeeID"].Value = employeeID; 
        command.Parameters["@CreateDate"].Value = createDate; 

        return command.ExecuteNonQuery(); 
       } 

     } 
     } 
     catch(Exception ex) 
     { 
      LogException(ex); 
      return either -1 or re throw exception. 
     } 
+1

+1。另外,我會拋出一個自定義的異常,比如'throw new MyCustomException(「Failed to create employee」,ex);' – 2010-04-29 12:05:08

+0

@klausbyskov這也是一個很好的觀點。 – kemiller2002 2010-04-29 12:05:35

+1

@odiseh你可以有一個catch塊,你沒有發現異常,但是你不知道哪裏出了問題。我一直認爲你必須對這個例外做些什麼。即使它發現了一個特殊的例外並忽略它。 – kemiller2002 2010-04-29 12:30:15

2

在我看來,這是完全錯誤的地方,趕上什麼,你不能處理那裏,然後。讓異常冒泡並讓調用應用程序實現它自己的錯誤處理。如果你在這裏捕獲併吞下一個異常,那麼你對已安裝應用程序的調試將會是噩夢。

只有能趕上你可以處理,並拋出一切......

0

我會做一個

catch(Exception ex){ 
    // log exception here and set return value 
} 
0

我喜歡通過收縮所有異常有選擇的幾個形式化我DAL的API,以及將實際例外嵌套爲InnerException

例如,所有不是調用者故障的數據庫異常都會拋出一種類型的異常,調用者發生故障的所有數據庫異常(如沒有選擇行,無效PK,無效數據)會拋出另一種類型的異常(或者甚至可能在異常類型之間有更細微的區別),以及與數據庫無關的東西(健全性檢查,NullRef等)的最後一個異常類型,除了我無法處理的異常(例如OutOfMemoryException)。

這種方式很容易捕捉到我在DAL中拋出的異常,並且所有特定的血腥細節仍可在InnerException中找到。

相關問題