C#捕获OracleException与常规异常

时间:2019-04-05 20:00:02

标签: c# oracle try-catch

我正在更新先前项目中的代码。我想捕获SQL命令中的所有问题,因此在SQL查询的开头放置了“ X”。在Microsoft中,通常SqlException将捕获此类问题。在下面的代码中,我得到了常规的Exception,而不是OracleException。我有适当的例外吗?如果是,为什么不解雇?

public static string getSKUByPONumber(string poNumber)
{
    Logging.logMethodStart("getSKUByPONumber poNumber=" + poNumber);

    string rt = "";
    string sql = "x select i.segment1 || '.' || i.segment2 " +
                 " from po_headers_all h " +
                 " inner join po.po_lines_all l on( h.po_header_id = l.po_header_id ) " +
                 " inner join inv.mtl_system_items_b i on( l.item_id = i.inventory_item_id ) " +
                 " where h.segment1 = '" + poNumber.Trim() + "'";

    try
    {
        rt = getOracleSingleValue(sql);
    }
    catch (OracleException ex)
    {
        string errMsg = "OracleException: getSKUByPONumber failed with error code: " + ex.Message + " Sql=" + sql;
        Logging.logHelperError("getSKUByPONumber", ex.Message);
        Logging.TraceWriteLine(errMsg); 
        throw new Exception(errMsg);
    }
    catch (Exception ex)
    {
        Logging.logHelperError("getSKUByPONumber", ex.Message);
        throw new Exception("getSKUByPONumber failed with error code: " + ex.Message + " Sql=" + sql );
    }

    Logging.logMethodEnd("getSKUByPONumber poNumber=" + poNumber + " sku=" + rt);
    return rt;
}

修改后的代码:

    public static string getOracleSingleValue(string sql)
    {
        OracleConnection conn = Config.GetOracleSqlconnection();
        string rt = "";

        try
        {
            DataSet ds = new DataSet();
            OracleDataAdapter adapter = new OracleDataAdapter(sql, conn);
            OracleCommandBuilder builder = new OracleCommandBuilder(adapter);

            adapter.Fill(ds, "data");

            if (ds.Tables["data"].Rows.Count > 0)
            {
                DataRow row = ds.Tables["data"].Rows[0];
                rt = row[0].ToString();
            }
        }
        catch (SqlException ex)
        {
            Logging.logHelperError("getOracleSingleValue: SqlException:", ex.Message);
            throw new Exception("getOracleSingleValue failed with SqlException: " + ex.Message);
        }
        catch (Exception ex)
        {
            Logging.logHelperError("getOracleSingleValue", ex.Message);
            throw new Exception("getOracleSingleValue failed with error code: " + ex.Message);
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }

        return rt;
    }



public static string getSKUByPONumber(string poNumber)
{
    Logging.logMethodStart("getSKUByPONumber poNumber=" + poNumber);

    string rt = "";
    string sql = "x select i.segment1 || '.' || i.segment2 " +
                 " from po_headers_all h " +
                 " inner join po.po_lines_all l on( h.po_header_id = l.po_header_id ) " +
                 " inner join inv.mtl_system_items_b i on( l.item_id = i.inventory_item_id ) " +
                 " where h.segment1 = '" + poNumber.Trim() + "'";

    try
    {
        rt = getOracleSingleValue(sql);
    }
        catch (SqlException ex)
        {
            string errMsg = "SqlException: getDropShipFlag failed with error code: " + ex.Message + " sql=" + sql;
            Logging.TraceWriteLine(errMsg);
            Logging.logHelperError("getDropShipFlag", ex.Message);
            throw new Exception(errMsg);
        }
        catch (Exception ex)
        {
            string innerExMessage = ""; 
            if (ex.InnerException != null)
            {
                innerExMessage = ex.Message + " Internal Exception:" + ex.InnerException; 
            }
            Logging.logHelperError("getDropShipFlag", ex.Message + innerExMessage);
            throw new Exception("Exception: getDropShipFlag failed with error code: " + ex.Message);
        }

    Logging.logMethodEnd("getSKUByPONumber poNumber=" + poNumber + " sku=" + rt);
    return rt;
}

最终错误是:

  

{“异常:getSKUByPONumber失败,错误代码:   getOracleSingleValue失败,错误代码:ORA-00900:无效的SQL   声明”}

1 个答案:

答案 0 :(得分:0)

问题是您正在调用自己的getOracleSingleValue()例程。 此例程必须引发异常,以便您捕获任何异常。 我怀疑这看起来像一个自定义(非Oracle)函数。 检查一下。