繁体   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