Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

c# - "Invalid attempt to call Read when reader is closed" error (for lengthy operations only)

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.

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?

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Could be a database timeout instead... you checked that?

What are you actually trying to do anyway? If you really need to read so many rows perhaps consider reducing it into smaller chunks - say, only read 1000 at a time


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...