我需要对每个命令都使用 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 值。因为例如 Command2Command3 工作正常。只是 Command1 出错。我可以像这样对每个命令使用 try-catch:

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

但是我有 300-400 个命令,我不能对每个命令都使用 try-catch(我可以,但是太难了)。如何使用 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();

如果你需要执行所有 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
   }
}

我希望你这个代码只是一个演示而不是你的实际代码。
这是一个很好的例子,说明如何不使用数据库。

一个快速简单的解决方案是用 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();

注意我建议的函数 returns 一个布尔值来表示成功。

当然,您可以创建一个字典来保存命令及其失败值,并使用 foreach 对其进行迭代。

然而

我必须指出你应该重新考虑你的设计。 300 次数据库调用 {300} 在单个网站中进行的数据库调用太多了,更不用说单个 aspx 页面了。

您可以试试下面的方法。 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;
    }