簡體   English   中英

我是否需要對每個命令使用try-catch?

[英]Do I need to use try-catch for every command?

我有oledb連接。 我使用try-catch,因為命令有時會出錯。 像這樣:

    OleDbConnection Connection;
    Connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
               Server.MapPath("~/db.mdb"));
    OleDbCommand Command1, Command2, Command3;

    Command1 = new OleDbCommand("SELECT a FROM Table1 WHERE ID = 1", Connection);
    Command2 = new OleDbCommand("SELECT a FROM Table1 WHERE ID = 2", Connection);
    Command3 = new OleDbCommand("SELECT a FROM Table1 WHERE ID = 3", Connection);

try
{
    Connection.Open();
    var1= (int)Command1.ExecuteScalar();
    var2= (int)Command2.ExecuteScalar();
    var3= (int)Command3.ExecuteScalar();
    Connection.Close()
}
catch (Exception)
{
var1 = 2;
var2 = 24;
var3 = 55;
}

但是,當我從Command1收到錯誤時,它會去捕獲並使用catch值。 我需要在捕獲該命令錯誤時使用catch值。 因為例如Command2Command3正常工作。 只是Command1出現錯誤。 我可以對每個命令使用try-catch,如下所示:

try
{
var1= (int)Command1.ExecuteScalar();
}
catch (Exception)
{
var1 = 2;
}
...

但是我有300-400個命令,我不能對每個命令都使用try-catch(我可以,但是很難)。 如何使用catch來獲取錯誤命令?

我希望您該代碼只是一個演示,而不是您的實際代碼。
這是一個很好的例子,說明了如何不使用數據庫。

一種快速,簡單的解決方案是將命令的執行與try catch塊一起封裝。 像這樣的東西:

bool TryGetInt(OleDbCommand Command, int ValueIfException, out int Value) 
{
    try
    {
        if(Connection.State == ConnectionState.Closed || Connection.State == ConnectionState.Broken) 
        {
          Connection.Open();
        }
         Value = (int)Command1.ExecuteScalar();
         return true;
    }
    catch (Exception)
    {
         // Consider writing the exception into a log file
         Value = ValueIfException;
         return false;
    }
}

並這樣稱呼它:

TryGetInt(Command1, 2, out var1);
TryGetInt(Command2, 24, out var2);
....
Connection.Close();

請注意,我建議的函數返回一個布爾值以指示成功。

當然,您可以創建一個字典來保存命令及其失敗值,並使用foreach對其進行迭代。

然而

我必須指出,您應該重新考慮您的設計。 {300} 300個數據庫調用是在單個網站中進行太多數據庫調用的方式 ,更不用說單個aspx頁面了。

您可以只編寫一個將命令字符串和catch值作為參數的函數,然后在其中執行try / catch處理邏輯。

您可以創建命令列表,將每個命令添加到列表中,然后循環調用。 像這樣:

        OleDbConnection Connection;
        Connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
                   Server.MapPath("~/db.mdb"));
        OleDbCommand Command1, Command2, Command3;

        List<OleDbCommand> commands = new List<OleDbCommand>();
        commands.Add(new OleDbCommand("SELECT a FROM Table1 WHERE ID = 1", Connection));
        commands.Add(new OleDbCommand("SELECT a FROM Table1 WHERE ID = 2", Connection));
        commands.Add(new OleDbCommand("SELECT a FROM Table1 WHERE ID = 3", Connection));

        Connection.Open();
        foreach (var command in commands)
        {
            try
            {
                var1= (int)Command.ExecuteScalar();
            }
            catch (Exception)
            {
                  // ...
            }
        }
        Connection.Close();

您可以嘗試以下類似方法。 2個選項。 一個模板,以防您不僅處理int。 但是您將禁止所有異常。 這通常不是一個好主意。 更好地捕獲特定的異常:

        OleDbCommand b = myCommand;
        int c = ExecuteScalarSuppressException(b, 24);
        int d = ExecuteScalarSuppressException<int>(b, 33);

    private static int ExecuteScalarSuppressException(OleDbCommand oleDbCommand, int defaultValue)
    {
        int returnValue = defaultValue;
        try
        {
            defaultValue = (int)oleDbCommand.ExecuteScalar();
        }
        catch (Exception)
        {
        }
        return defaultValue;
    }

    private static T ExecuteScalarSuppressException<T>(OleDbCommand oleDbCommand, T defaultValue)
    {
        T returnValue = defaultValue;
        try
        {
            defaultValue = (T)oleDbCommand.ExecuteScalar();
        }
        catch (Exception)
        {
        }
        return defaultValue;
    }

如果您需要執行所有300/400命令,我寧願有這樣的內容:

for(int i=0;i<300;i++)
{
   Command1 = new OleDbCommand("SELECT a FROM Table1 WHERE ID = "+i, Connection);
   try{
       Connection.Open();
       var1= (int)Command1.ExecuteScalar();
   }catch(Exception e)
   {
        //your logic here
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM