OutOfMemoryException when using SQL Filestream

北城以北 提交于 2019-12-21 06:18:29

问题


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.

Here's my code snippet -

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?

Thanks!


回答1:


Knowing where exactly the error is happening could go a long way in helping to find the solution. One change to make that may help is to chunk the file writing (don't write it all at once, loop and write a little bit at a time). This gives the stream a chance to flush and therefore release system resources.




回答2:


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();
    }
}


来源:https://stackoverflow.com/questions/4441179/outofmemoryexception-when-using-sql-filestream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!