首页 > 解决方案 > 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());
}

标签: c#.netmariadbodbc

解决方案


将 try 块放在 OdbcCommand 上方,或者在打开连接的位置更好。因为您遇到的错误发生在任何异常处理之前,这就是应用程序崩溃的原因。


推荐阅读