“阅读器关闭时调用读取的尝试无效"错误(仅适用于冗长的操作)

quot;Invalid attempt to call Read when reader is closedquot; error (for lengthy operations only)(“阅读器关闭时调用读取的尝试无效错误(仅适用于冗长的操作))
本文介绍了“阅读器关闭时调用读取的尝试无效"错误(仅适用于冗长的操作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个操作,其中从 csv 文件中读取超过 100.000 条记录并插入到数据库中.当我使用一个有 10 条记录的文件时,操作在不到一分钟的时间内成功完成.

We have a operation in which more than 100.000 records are read from a csv file and inserted in a database. When I am using a file with 10 records, the operation is completed successfully in less than one minute.

当我使用 100.000 条记录时,我收到以下错误读取器关闭时调用读取无效".10分钟后.是否可以配置任何超时来避免此错误?

When I use 100.000 records, I am getting the following error "Invalid attempt to call Read when reader is closed." after 10 minutes. Is there any Timeout that I can configure to avoid this error?

注意:CommandTimeout 已经设置为零.

Note: The CommandTimeout is already set as zero.

DbCommand cmd = db.GetStoredProcCommand("aspInsertZipCode");
cmd.CommandTimeout = 0;
dataStringToProcess.Remove(dataStringToProcess.Length - 1, 1);

db.AddInParameter(cmd, "@DataRows", DbType.String, dataStringToProcess.ToString());
db.AddInParameter(cmd, "currDate", DbType.DateTime, DateTime.Now);
db.AddInParameter(cmd, "userID", DbType.Int32, UserID);
db.AddOutParameter(cmd, "CountOfUnchangedZipCode", DbType.String, 1000);

DbDataReader rdr = null;

try
{

    rdr = (DbDataReader)db.ExecuteReader(cmd);
    if (rdr.Read())
    {

        if (!String.IsNullOrEmpty(Utility.GetString(rdr, "NewZipCode")))
            strNewZipCode = strNewZipCode + "," + Utility.GetString(rdr, "NewZipCode");

    }
    rdr.NextResult();
    if (rdr.Read())
    {

        strRetiredZipCode = strRetiredZipCode + "," + Utility.GetString(rdr, "RetiredZipCode");

    }

    int TempUnchageZipCount = Convert.ToInt32(db.GetParameterValue(cmd, "CountOfUnchangedZipCode"));
    CountOfUnchangedZipCode = CountOfUnchangedZipCode + TempUnchageZipCount;
    dataStringToProcess = new StringBuilder();
    cntRec = 0;

}
catch
{
    if (rdr != null && (!rdr.IsClosed))
        rdr.Close();
    throw;
}
finally
{
    if (rdr != null && (!rdr.IsClosed))
        rdr.Close();
}
cmd.Dispose();

推荐答案

原来是MSDTC &交易问题.我更新了组件服务中的 MSDTC 超时.我还更新了应用程序服务器 machine.config 中 System.Transactions 中的超时值.

It was actually MSDTC & Transaction issue. I updated the MSDTC timeout in Component Services. Also I updated timeout values in System.Transactions in machine.config of the application server.

这篇关于“阅读器关闭时调用读取的尝试无效"错误(仅适用于冗长的操作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)