簡體   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