使用SQL Filestream时出现OutOfMemoryException [英] OutOfMemoryException when using SQL Filestream
本文介绍了使用SQL Filestream时出现OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一个约600 MB的zip文件上传到SQL 2008 FILESTREAM表,但出现OutOfMemoryException.我正在使用SqlFileStream类上传文件(如本教程中所述-
I'm trying to upload a zip file which is around 600 MB to SQL 2008 FILESTREAM table and I get the OutOfMemoryException. I'm using the SqlFileStream class to upload the file (as described in this tutorial - http://www.aghausman.net/dotnet/saving-and-retrieving-file-using-filestream-sql-server-2008.html). I have a 32-bit Vista machine with 4GB ram if that matters and I'm using VS 2010, Entity Framework 4.
这是我的代码段-
public static void AddItem(RepositoryFile repository)
{
var contents = repository.Data; // I get the exception at this line.
repository.Data = System.Text.Encoding.ASCII.GetBytes("0x00");
using (var scope = new TransactionScope())
{
using (var db = new MyEntities())
{
db.RepositoryTable.AddObject(repository);
db.SaveChanges();
}
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var cmd = new SqlCommand("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable", con))
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var path = reader.GetString(0);
var transactionContext = reader.GetSqlBytes(1).Buffer;
var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);
fileStream.Write(contents, 0, contents.Length);
fileStream.Close();
}
}
}
scope.Complete();
}
}
如何上传文件而没有任何错误?
How do I upload the file without any errors?
谢谢!
推荐答案
我发现了问题所在.这是我的代码-
I figured out the issue. Here's my code -
private void AddFile()
{
if (!fupFile.HasFile)
{
lblMessage.Text = "Please select a file.";
return;
}
var data = new byte[(int) fupFile.FileContent.Length];
fupFile.FileContent.Read(data, 0, data.Length);
if (fupFile.FileContent.Length > 0)
{
var repositoryFile = new Repository
{
ID = Guid.NewGuid(),
Name = Path.GetFileName(fupFile.PostedFile.FileName),
Data = System.Text.Encoding.ASCII.GetBytes("0x00")
};
RepositoryController.AddItem(repositoryFile, data); // Calling DAL class.
}
}
// DAL method
public static void AddItem(RepositoryFile repository, byte[] data)
{
using (var scope = new TransactionScope())
{
using (var db = new MyEntities()) // DBContext
{
db.RepositoryTable.AddObject(repository);
db.SaveChanges();
}
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(string.Format("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable WHERE ID='{0}'", repository.ID), con)) // "Data" is the column name which has the FILESTREAM. Data.PathName() gives me the local path to the file.
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var path = reader.GetString(0);
var transactionContext = reader.GetSqlBytes(1).Buffer;
var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);
fileStream.Write(contents, 0, contents.Length); //Write contents to the file.
fileStream.Close();
}
}
}
scope.Complete();
}
}
这篇关于使用SQL Filestream时出现OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文