我怎么知道在OracleCommand Insert上哪个参数导致FormatException(“输入字符串格式不正确”)?

时间:2021-08-06 06:52:38

I have a problem in which I have a huge insert with tons of parameters.

我有一个问题,我有一个巨大的插入与大量的参数。

I am using the OracleCommand object to add my parameters prior on executing the query.

我正在使用OracleCommand对象在执行查询之前添加我的参数。

Later I execute ExecuteNonQuery() method and it gives the FormatException ("Input string was not in a correct format").

后来我执行了ExecuteNonQuery()方法,它给出了FormatException(“输入字符串的格式不正确”)。

My question is, is there a way I can know which parameter is causing the Exception?

我的问题是,有没有办法可以知道哪个参数导致异常?

My code looks something like this

我的代码看起来像这样

using (OracleConnection conn = new OracleConnection(_connString))

{

      conn.open();

      using(var cmd = conn.CreateCommand())

      {
          cmd.CommandType = CommandType.Text;

          cmd.CommandText = insert (..., ... ,...) into ..... values (nameOfParameter, ..., .... ,...);

          cmd.Parameters.Clear();


          cmd.Parameters.Add("nameOfParameter", OracleDBType.SomeTypeOfData, valueOfParameter, ParameterDirection.Input)

          //Lots of Parameter Adding

          cmd.ExecuteNonQuery();
      }


}

2 个解决方案

#1


1  

You need to check every single parameter you are passing to your method.

您需要检查传递给方法的每个参数。

if (parameter1 == null)
    logger.debug("parameter1 is null");
if (parameter2 == null)
    logger.debug("parameter2 is null");
// ...
// You may do something else rather than only logging.

#2


1  

You can enclose your code in a

您可以将代码括在一个

try { 
     // entire db transaction code goes here
} catch(Exception e) {
    // you can log the exception and (cross finger) will give you more details 
}

#1


1  

You need to check every single parameter you are passing to your method.

您需要检查传递给方法的每个参数。

if (parameter1 == null)
    logger.debug("parameter1 is null");
if (parameter2 == null)
    logger.debug("parameter2 is null");
// ...
// You may do something else rather than only logging.

#2


1  

You can enclose your code in a

您可以将代码括在一个

try { 
     // entire db transaction code goes here
} catch(Exception e) {
    // you can log the exception and (cross finger) will give you more details 
}