小编Pau*_*lie的帖子

超时异常导致SqlDataReader关闭?

我正试图从数据库中提取一些二进制数据并将其写入pdf文件.在大多数情况下,这是在游泳,但偶尔的数据行似乎抛出一个特定的错误 -

超时已过期.操作完成之前经过的超时时间或服务器没有响应.

请记住,这只发生在少数行上,并且从不随机.相同的行总是抛出异常.我不确定为什么会抛出异常,但我可以跳过导致问题并继续前进的行.然而,我的问题是,当我捕获异常然后尝试移动到下一行时,我遇到另一个异常 -

InvalidOperationException - 读取器关闭时无效尝试调用Read.

这是否意味着读者一旦遇到异常就会自动关闭?如何在没有任何戏剧的情况下继续前进到下一行?

        while (sdrReader.Read()) // Second exception happens here
        {
            try
            {
                byte[] byteData = new Byte[(sdrReader.GetBytes(0, 0, null, 0, int.MaxValue))]; // first exception happens here
                sdrReader.GetBytes(0, 0, byteData, 0, byteData.Length);
                string strOutputFileName = sdrReader.GetInt32(1).ToString() + ".pdf";
                msMemoryStreams = new MemoryStream();
                msMemoryStreams.Write(byteData, 0, byteData.Length);
                byte[] byteArray = msMemoryStreams.ToArray();

                msMemoryStreams.Flush();
                msMemoryStreams.Close();

                writeByteArrayToFile(byteData, txtFilesPath.Text + "\\" + strOutputFileName);
            }
            catch (Exception e)
            {
                Logger.Write("Document failed to convert: " + e.Message);
            }
        }
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪,根据要求 -

   at …
Run Code Online (Sandbox Code Playgroud)

c# sql sqldatareader

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

sql ×1

sqldatareader ×1