我正在尝试查询数据库并提取可能存储为100 万行(~200MB)varbinary
的 excel 文件并将其传递给验证器。
我们的构建服务器有6GB的内存和负载平衡的处理器,在运行时几乎不会耗尽 CPU 或内存。
然而,大约 40 秒后,该过程会抛出一个OutOfMemoryException
.
这是堆栈跟踪:
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Data.SqlTypes.SqlBinary.get_Value()
at System.Data.SqlClient.SqlBuffer.get_ByteArray()
at System.Data.SqlClient.SqlBuffer.get_Value()
at System.Data.SqlClient.SqlDataReader.GetValueFromSqlBufferInternal(SqlBuffer data, _SqlMetaData metaData)
at System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i)
at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)
at System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
at eConfirmations.DataService.FileServices.FileDataService.GetFileContent(Guid fileId) in d:\w1\3\s\Source\eConfirmations.DataService\FileServices\FileDataService.cs:line 157
...
at System.Data.SqlTypes.SqlBinary.get_Value()
at System.Data.SqlClient.SqlBuffer.get_ByteArray()
at System.Data.SqlClient.SqlBuffer.get_Value()
at System.Data.SqlClient.SqlDataReader.GetValueFromSqlBufferInternal(SqlBuffer data, _SqlMetaData metaData)
at System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i)
at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)
at System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue)
at System.Data.SqlClient.SqlCommand.ExecuteScalar()
at eConfirmations.DataService.FileServices.FileDataService.GetFileContent(Guid fileId) in d:\w1\3\s\Source\eConfirmations.DataService\FileServices\FileDataService.cs:line 157
这是引发异常的我的代码:
private byte[] GetFileContent(Guid fileId)
{
byte[] content;
string connectionString = ConfigurationManager.ConnectionStrings["eConfirmationsDatabase"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandTimeout = 300;
sqlCommand.CommandText = $"SELECT Content FROM dbo.[File] WHERE FileId = '{fileId}'";
sqlConnection.Open();
content = sqlCommand.ExecuteScalar() as byte[];
sqlConnection.Close();
sqlCommand.Dispose();
}
sqlConnection.Dispose();
}
return content;
}
有没有更有效的方法来拉回这些数据,或者我们可以更新我们的构建服务器上的设置以避免这个错误?