简体   繁体   English

在C#中的try / catch中未捕获数据库异常

[英]Database exception not being caught in try/catch in c#

I'm working with a DB2 database using an OdbcConnection in c#. 我正在使用C#中的OdbcConnection处理DB2数据库。 I have never seen a sql exception thrown by the database never get caught, but sure enough, I've now run into this occurrence. 我从未见过数据库抛出的sql异常从未被捕获,但是可以肯定的是,我现在遇到了这种情况。 My statements to open & close the connection work fine according to my print statements. 根据我的打印陈述,我打开和关闭连接的陈述可以正常工作。 I'm surrounding the code in a try/catch and am writting to the log file at various points in the try/catch body as well as in the exception body. 我将代码放在try / catch中,并在try / catch主体以及异常主体中的各个点写入日志文件。 My log statements in the exception body are never written...by all accounts from my logging, the database insert went fine...only it didn't as far as DB2 is concerned. 我在异常主体中的日志语句从未被写入过……从我的日志记录中的所有帐户来看,数据库插入都很好……只是就DB2而言并没有。

Here is an interesting part of the trace I'm seeing: 这是我看到的跟踪中有趣的部分:

[1380564032.174591 - 09/30/2013 13:00:32.174591] ( Unretrieved error message="[IBM][CLI Driver][DB2/AIX64] SQL0407N Assignment of a NULL value to a NOT NULL column "TBSPACEID=2, TABLEID=4, COLNO=7" is not allowed. SQLSTATE=23502 [1380564032.174591-09/30/2013 13:00:32.174591](未检索到的错误消息=“ [IBM] [CLI驱动程序] [DB2 / AIX64] SQL0407N将空值分配给非空列” TBSPACEID = 2,TABLEID = 4,不允许COLNO = 7“。SQLSTATE = 23502

" ) [1380564032.186504 - 09/30/2013 13:00:32.186504] ”)[1380564032.186504-2013/9/30 13:00:32.186504]

I was initially using ExecuteScalar. 我最初使用ExecuteScalar。 I've tried ExecuteNonQuery & ExecuteReader. 我试过ExecuteNonQuery和ExecuteReader。 All have the same error and this same issue with the error not being caught in the try/catch. 它们都具有相同的错误和相同的问题,而该错误未在try / catch中捕获。

Thoughts? 有什么想法吗?

EDIT 1 编辑1

Here's the code: 这是代码:

public void InsertRecord(MyObject entry)
{
    try
    {

        OpenConnection();

        // Prep command object.             
        using (OdbcCommand cmd = new OdbcCommand(Queries.InsertRecord, this.odbcCn))
        {
            cmd.CommandTimeout = 30;
            cmd.CommandType = CommandType.Text;

            //Force error testing
            entry.Zip = "9999999999999999999999999999999999999999999999999999999999999999";

            cmd.Parameters.Add(new OdbcParameter("@ZIP", ntry.Zip));

            object o = cmd.ExecuteReader(CommandBehavior.SingleResult);

            int id = -1;

            LogUtil.WriteCondensed("Insert to db succeeded. Id: " + id.ToString());
        }
    }
    catch (OdbcException e)
    {
        LogUtil.Write("INSERT ROW", "Exception occurred");
        CloseConnection();
        throw;
    }
    catch (Exception e)
    {
        LogUtil.Write("INSERT ROW","Exception occurred");
        CloseConnection();
        throw;
    }
    finally
    {
        CloseConnection();
    }

    return ;
}

Found my answer here: 在这里找到我的答案:

DB connection InfoMessage callback not firing immediately for stored proc print statements 数据库连接InfoMessage回调不会立即为存储的proc打印语句触发

I'm not having the issues the person posting the question was. 我没有发布问题的人的问题。 I just had to provide a handler for the InfoMessage delegate. 我只需要为InfoMessage委托提供处理程序即可。 My handler looks like: 我的处理程序看起来像:

    private static void OnInfoMessage(Object sender, OdbcInfoMessageEventArgs args)
    {
        StringBuilder sb = new StringBuilder();

        if (args != null && args.Errors != null &&
            args.Errors.Count > 0)
        {
            foreach (OdbcError error in args.Errors)
            {
                sb.AppendLine(error.Message);
            }

            LogUtil.WriteException("InsertRow", new Exception(sb.ToString()));
        }

        if (!String.IsNullOrEmpty(args.Message))
        {
            LogUtil.Write("InsertRow", args.Message);
        }

    }

Thanks for your input, everyone. 谢谢大家的投入。 Is there anything you'd like to add? 您有什么要补充的吗? Note that I'm using ExecuteReader on the insert statement, but the error isn't being thrown natively without giving the connection delegate this handler. 请注意,我在insert语句上使用了ExecuteReader,但是如果不给连接委托此处理程序,就不会在本地引发错误。 thoughts? 有什么想法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM