读取大型 .accdb 文件引发异常“现有连接被远程主机强行关闭”

Posted

技术标签:

【中文标题】读取大型 .accdb 文件引发异常“现有连接被远程主机强行关闭”【英文标题】:Reading large .accdb file raise exception "An existing connection was forcibly closed by the remote host" 【发布时间】:2013-03-01 08:50:17 【问题描述】:

我调试了一些旧的 ASP.NET 应用程序,它试图读取一个巨大的 .accdb 文件(大约 550 000 行)。代码如下:

OleDbConnection sourceConnection = new OleDbConnection(
            "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + destinationFile);
        OleDbDataAdapter sourceAdapter = new OleDbDataAdapter("select * from [" + tableName + "]", sourceConnection);
        DataSet sourceDataSet = new DataSet();
        try
        
            sourceConnection.Open();
            sourceAdapter.AcceptChangesDuringFill = false;
            sourceAdapter.Fill(sourceDataSet, tableName);
        
        catch (Exception ex)
        
            _trace.TraceEvent(TraceEventType.Error, 0, ex.Message);
        
        finally
        
            sourceConnection.Close();
        

在某些时候我遇到了异常:

“无法从传输连接中读取数据:现有连接被远程主机强行关闭”

Server stack trace: 
at     System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Access2010.IAccessService.LoadFromMDB(String destinationFile, String tableName)
at Access2010.AccessService.LoadFromMDB(String destinationFile, String tableName)

有人知道它可能是什么以及如何解决它吗? 据我了解,重写 SQL 查询以按小块检索数据的最佳选择。

【问题讨论】:

【参考方案1】:

我只需使用 access 中内置的升迁向导将其全部放入 SQL Express 数据库,然后更改连接字符串。

旁注,不要这样做:

"select * from [" + tableName + "]"

改为参数化查询。

【讨论】:

以上是关于读取大型 .accdb 文件引发异常“现有连接被远程主机强行关闭”的主要内容,如果未能解决你的问题,请参考以下文章

C# 从 Access .accdb 读取附件

级联组合框的 Visual Basic ACCDB 文件读取

32 位 pyodbc 读取 64 位访问 (accdb)

使用 Microsoft Excel VBA 仅查询访问 accdb 文件时,“MSysObjects”没有读取权限(错误编号:-2147217911) - mdb 工作正常

如何将 accdb 数据库从 Access 导入 SQL Server 2019?

Scala:从 scylla 读取数据引发异常