oracle ExecuteNonQuery 不会在插入时引发异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1833539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExecuteNonQuery doesn't throw exception on insert
提问by Elz
I'm using OracleCommand.ExecuteNonQuery() to insert into a table. Everything works fine but occasionally records are not inserted. So is it possible that ExecuteNonQuery() doesn't insert the record and also doesn't throw an exception?
我正在使用 OracleCommand.ExecuteNonQuery() 插入到表中。一切正常,但偶尔没有插入记录。那么 ExecuteNonQuery() 是否有可能不插入记录并且也不抛出异常?
I'm not using stored procedures. I'm not using a transaction. I am logging any exception that is thrown by ExecuteNonQuery() but apparently no exception is thrown... I am also notchecking the return value of ExecuteNonQuery(). But is it possible that ExecuteNonQuery returns another value than 1 if the insert is successful?
我没有使用存储过程。我没有使用事务。我正在记录 ExecuteNonQuery() 抛出的任何异常,但显然没有抛出异常......我也没有检查 ExecuteNonQuery() 的返回值。但是,如果插入成功,ExecuteNonQuery 是否有可能返回 1 以外的其他值?
回答by Hasani Blackwell
It shouldn't. ExecuteNonQuery returns an integer indicating the number of rows affected. If no rows were inserted, 0 should be returned. It should thrown an exception when it fails to execute the query. E.x.: the connection is closed, the table doesn't exist, etc.
它不应该。ExecuteNonQuery 返回一个整数,指示受影响的行数。如果未插入任何行,则应返回 0。当它无法执行查询时,它应该抛出异常。例如:连接已关闭,表不存在等。
回答by Neil Barnwell
Not unless you're swallowing the exception somewhere, like this:
除非你在某处吞下异常,像这样:
try
{
DoSomethingOnTheDbThatMightThrowAnException();
}
catch(Exception ex)
{
// Do nothing, thus swallowing the exception
}
Also, check the return value of ExecuteNonQuery()
. If it is 0, you might want to consider throwing an exception yourself.
另外,请检查 的返回值ExecuteNonQuery()
。如果它是 0,您可能要考虑自己抛出异常。
回答by Gary Myers
"Everything works fine but occasionally records are not inserted." Possibly inserting into/through a view which can allow you to insert records which you can't actually see. Virtual Private Database/Row Level Security can do this too.
“一切正常,但偶尔没有插入记录。” 可能插入/通过一个视图,它可以让你插入你实际上看不到的记录。虚拟专用数据库/行级安全也可以做到这一点。
回答by Bravax
It's quite unlikely.
这不太可能。
Do you have a error handler at a higher level which might be catching the error, and throwing it away?
您是否有更高级别的错误处理程序可能会捕获错误并将其丢弃?
回答by MIke
Just for the record I had this issue.
只是为了记录我有这个问题。
It occurred because I was testing for a null or DBNull.Value from a gridviewcell in a row when building the Command parameters for the stored proc from the cells.
发生这种情况是因为在从单元格构建存储过程的命令参数时,我正在连续测试 gridviewcell 中的 null 或 DBNull.Value。
Something like this:
像这样的东西:
if(cell.Value != DBNull.Value && cell.Value != null)
{
//add the parameter to the Command object
}
This meant that it was creating a parameter when I got an empty string and the Convert.ToDecimal didn't throw the NumberFormatException at that time.
这意味着当我得到一个空字符串并且 Convert.ToDecimal 没有抛出 NumberFormatException 时,它正在创建一个参数。
Instead the NumberFormatException got thrown when I tried to execute the cmd.ExecuteNonQuery().
相反,当我尝试执行 cmd.ExecuteNonQuery() 时抛出了 NumberFormatException。
I'm guessing that there is a conversion which takes place at runtime when the ExecuteNonQuery() is kicked off.
我猜当 ExecuteNonQuery() 启动时,会在运行时发生转换。
I fixed it by switching to:
我通过切换到:
if(cell.Value != DBNull.Value &&
!string.IsNullOrEmpty(cell.Value.ToString()))
{
//Create and Add the parameter
}
回答by Philip Atz
It could be that ExecuteNonQuery() throws an exception that is not caught by your try-catch block. Check your Windows Event log to be sure; an AccessViolationException could occur due to one of your drivers crashing and that sort of exception is not normally catchable in managed code from .NET 4.0 onwards. A solution is described on this link.
可能是 ExecuteNonQuery() 抛出了一个没有被你的 try-catch 块捕获的异常。检查您的 Windows 事件日志以确保;AccessViolationException 可能由于您的驱动程序之一崩溃而发生,并且从 .NET 4.0 开始,通常无法在托管代码中捕获这种异常。此链接中描述了解决方案。