c# - ODBC ExecuteNonQuery() 挂起
问题描述
我有一个有趣的问题。我有一个数据连接器,我通过它对数据库执行 INSERT 和 UPDATE 命令。问题是有时一切运行正常并且我的查询被插入,但有时 ExecuteNonQuery 方法只是冻结并且我的数据连接器只是停止并且它不会抛出任何异常也不会抛出任何超时。有一个代码片段:
OdbcCommand insertCommand = dbConnection.CreateCommand();
insertCommand.CommandText = @"INSERT INTO `table`
(name, creation, modified, modified_by)
VALUES (
?, ?, ?, ?)";
insertCommand.Parameters.AddWithValue("@name", operation.ExternalProductionOrderLineOperationId);
insertCommand.Parameters.AddWithValue("@produced_qty", Convert.ToDecimal(operation.Quantity));
insertCommand.Parameters.AddWithValue("@product_operation_type", productionOrder.ProductOperationType);
insertCommand.Parameters.AddWithValue("@product_articulus", productionOrder.ProductArticulus);
_logger.LogInformation("Operacija paruosta exportinimui!"); <----------- THIS IS THE PLACE, sometimes it runs okey, sometimes it freezes
try
{
var n = insertCommand.ExecuteNonQuery();
if (n != 0)
{
await UpdateProductionOrderLineOperation(productionOrder, operation, true);
_logger.LogInformation("operation nr: {x} exported", counter);
}
else
{
_logger.LogInformation("operation nr: {x} failed to export");
}
}
catch (InvalidOperationException e)
{
_logger.LogInformation("InvalidOperationException: {x}", e.ToString());
}
catch (Exception e)
{
_logger.LogInformation("Exception: {x}", e.ToString());
}
}
catch (OdbcException e)
{
_logger.LogInformation("OdbcException: {x}", e.ToString());
}
catch (Exception e)
{
_logger.LogInformation("Exception: {x}", e.ToString());
}
解决方案
将 try 块放在 OdbcCommand 上方,或者在打开连接的位置更好。因为您遇到的错误发生在任何异常处理之前,这就是应用程序崩溃的原因。
推荐阅读
- javascript - Axios 错误请求 400 发布
- gnu-make - Gnu make,多组分组目标
- c# - 试图从 azure blob 下载 word 文档
- python-sphinx - 如何使用 Sphinx 构建开发版本 阅读文档主题
- excel - VBA:根据条件在值上方添加空白行
- php - 尝试使用 php 将数据插入 MSSQL 服务器但出现错误
- c# - HOWTO 知道 ASP.NET MVC 中的传入请求是由用户交互(用户登录)还是由 asp.net MVC 应用程序本身触发的
- android - 在 android 上使用附加文本调用 USSD
- excel - Excel 链接到 Quickbooks 中的页面
- python - 使用 Python 的 SQLite 的正确语法