繁体   English   中英

SQLFileStream .Write异常 - 句柄无效

[英]SQLFileStream .Write Exception - The Handle is Invalid

我在我们的App Server上有一个上传的文件,然后我使用SQLFileStream流入一个MSSQL数据库,写入10mb(我试过增加/减少这个)块。 这适用于500mb以下的文件,但是任何更大的文件都会出现错误The Handle is Invalid filestream.Write(buffer, 0, buffer.Length); 当它达到800mb左右的文件。

我假设有些东西在某个地方超时 - 有人能指出我正确的方向吗? 方法如下

protected bool TransferFileToDB(string DocID, string filePath)
{
    bool success = false;

    // Get the file from the Upload Folder
    FileInfo fi = new System.IO.FileInfo(filePath);

    string conStr = GetConnectionString();
    SqlConnection connection = new SqlConnection(conStr);

    if (fi.Exists)
    {
        try
        {
            byte[] buffer;
            long position = 0;
            int packetSize = 10 * 1000 * 1024; // 10mb chunks 

            // Open the file for reading
            using (FileStream fs = new FileStream(filePath, System.IO.FileMode.Open))
            {
                long length = fs.Length;

                using (TransactionScope transactionScope = new TransactionScope())
                {
                    string filePath = GetDocumentPath(DocID);

                    connection.Open();

                    SqlCommand sqlCommand = connection.CreateCommand();
                    sqlCommand.CommandText = "Select GET_FILESTREAM_TRANSACTION_CONTEXT()As TransactionContext";
                    byte[] transactionContext1 = (byte[])sqlCommand.ExecuteScalar();

                    using (SqlFileStream filestream = new SqlFileStream(filePath, transactionContext1, FileAccess.Write))
                    {
                        SqlParameter param = new SqlParameter();

                        while (position < length)
                        {
                            if (position + packetSize > length)
                            {
                                buffer = new byte[length - position];
                            }
                            else
                            {
                                buffer = new byte[packetSize];
                            }

                            //Read file
                            fs.Read(buffer, 0, buffer.Length);

                            //Copy to DB Server
                            filestream.Write(buffer, 0, buffer.Length);

                            filestream.Flush();

                            Debug.WriteLine("Copied to DB " + position.ToString());

                            position += buffer.Length;

                        }

                        filestream.Close();
                    }

                    transactionScope.Complete();
                }

                fs.Close();

            }

            // Once file has been sent to DB server, delete temp file on the App Server
             fi.Delete();

            return true;
        }
        catch (Exception e)
        {
            Debug.WriteLine("Error in TransferFileToDB" + e.Message + e.InnerException);
            fi.Delete();
            return false;
        }

最后想出了这个,所以这里是解决方案,以防任何人遇到它。

TransactionScope似乎有超过60秒的默认时间,因此在构造函数中传递时间值解决了我的所有问题

using (TransactionScope transactionScope = new TransactionScope
           (TransactionScopeOption.Required, TimeSpan.FromHours(1)))

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.110).aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM