gpt4 book ai didi

c# - 超时异常导致SqlDataReader关闭?

转载 作者:太空狗 更新时间:2023-10-29 17:42:46 27 4
gpt4 key购买 nike

我正在尝试从数据库中提取一些二进制数据并将它们写入 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);
}
}

堆栈跟踪,根据要求 -

   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByteArray(Byte[] buff, Int32 offset, Int32 len)
at System.Data.SqlClient.TdsParser.ReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, Int32 length, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ReadColumnData()
at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout)
at System.Data.SqlClient.SqlDataReader.GetSqlBinary(Int32 i)
at System.Data.SqlClient.SqlDataReader.GetBytesInternal(Int32 i, Int64 dataIndex, Byte[] buffer, Int32 bufferIndex, Int32 length)
at System.Data.SqlClient.SqlDataReader.GetBytes(Int32 i, Int64 dataIndex, Byte[] buffer, Int32 bufferIndex, Int32 length)
at Pdf2Rtf.Form1.Read() in F:\Code\Pdf2Rtf\Pdf2Rtf\Pdf2Rtf\Form1.cs:line 77
at Pdf2Rtf.Form1.btnRead_Click(Object sender, EventArgs e) in F:\Code\Pdf2Rtf\Pdf2Rtf\Pdf2Rtf\Form1.cs:line 24
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Pdf2Rtf.Program.Main() in F:\Code\Pdf2Rtf\Pdf2Rtf\Pdf2Rtf\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

最佳答案

看起来您的 SqlCommand 超时了 - 当您调用 ExecuteReader 时,关联的命令保持打开状态并且在您完成阅读之前很容易超时。正如它在 SqlCommand.CommandTimeout 中所说的那样文档:

This property is the cumulative time-out for all network reads during command execution or processing of the results. A time-out can still occur after the first row is returned, and does not include user processing time, only network read time.

当命令超时时,它会关闭阅读器,您无法从中恢复。

尝试解决此问题的第一件事是大幅增加 CommandTimeout,以确保您可以继续。

接下来,如果您还没有这样做,使用 ExecuteReader 重载可能会有所帮助,它允许您指定 CommandBehavior,并传递 CommandBehavior .SequentialAccess(根据 MSDN 主题 "Retrieving Large Data (ADO.NET) 中的建议)。

最后,您还可以尝试将读取分解为记录 block 。

关于c# - 超时异常导致SqlDataReader关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1719305/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com