2017-06-03 59 views

回答

2

我會強烈建議不要存儲在視頻保存在數據庫的SQL Server的視頻保存在數據庫的SQL Server視頻你的數據庫是由於以下原因:

1). Doing so would bloat your database size 
2). Slow down the performance of the database 
3). It opens your application to Denial of Service attacks (DDOS). 

你應該期待中的文件存儲的專用存儲您的視頻,如:

1). Cloud Storage. 
2). FTP storage. 
3). Server's file system. 
4). Etc... 

若y ou確實希望將視頻存儲在數據庫中以便進行測試或基準測試目的,您可以嘗試以下代碼:

using System; 
using System.Data; 
using System.Data.SqlClient; 

namespace Portal.WebApp.Models 
{ 
    public class DbBlobSaver 
    { 
     #region Public Methods 
     public void Execute(byte[] fileBytes) 
     { 
      using (var dbConnection = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI")) 
      { 
       var command = BuildCommand(dbConnection); 
       var blobParameter = BuildParameter(fileBytes); 

       command.Parameters.Add(blobParameter); 

       try 
       { 
        command.Connection.Open(); 
        command.ExecuteNonQuery(); 
       } 
       catch (Exception ex) 
       { 
        // Log errors here 
       } 
       finally 
       { 
        command.Connection.Close(); 
       } 
      } 
     } 
     #endregion 

     #region Private Methods 
     private SqlParameter BuildParameter(byte[] fileBytes) 
     { 
      var blobParameter = new SqlParameter("@BlobFile", SqlDbType.Binary); 

      blobParameter.Value = fileBytes; 

      return blobParameter; 
     } 

     private SqlCommand BuildCommand(SqlConnection connection) 
     { 
      var command = new SqlCommand(); 
      command.Connection = connection; 
      command.CommandType = CommandType.Text; 
      command.CommandText = "INSERT INTO BlobsTable(BlobFile)" + "VALUES (@BlobFile)"; 

      return command; 
     } 
     #endregion 
    } 
} 
+1

非常感謝你 –

+0

@AhmedRashed給予答案upvote肯定會幫助社區!也謝謝:) –

+1

將文件保存爲文件也提高了可玩性。 –

相關問題