2014-01-31 53 views
0

我有場景,我想存儲視頻到數據庫和網格顯示從數據庫下載,我會上傳視頻通過fileupload控制,它應該是sql server2008數據庫,這是最好的方式在C#和asp.net?如何使用c#將視頻保存到數據庫?

+1

不要將視頻存儲在數據庫中。將名稱存儲在表格中並上傳視​​頻文件夾。我覺得這是最好的方法。 – Shashank

回答

2

有很多鏈接它。看看這個。

http://weblogs.asp.net/hajan/archive/2010/06/21/save-and-display-youtube-video-links-on-asp-net-website.aspx

http://forums.asp.net/t/1104451.aspx/1?How+to+retrieve+video+file+from+sql+server+database http://www.saurabhdeveloper.com/techtips_details.php?tipsid=15 http://forums.asp.net/p/1533758/3719583.aspx http://forums.asp.net/t/1045855.aspx/2/10 http://forums.asp.net/t/1511588.aspx/1 http://www.dotnetspider.com/forum/274821-Play-video-file-asp-net-page.aspx http://blogs.ugidotnet.org/kfra/archive/2006/10/04/50003.aspx http://www.dotnetspider.com/resources/16239-code-for-video-upload.aspx

http://www.c-sharpcorner.com/Forums/Thread/88899/ http://www.asp.net/webmatrix/tutorials/10-working-with-video

http://www.c-sharpcorner.com/Forums/Thread/88899/

試試這個代碼

byte[] buffer; 
//this is the array of bytes which will hold the data (file) 

SqlConnection connection; 
protected void ButtonUpload_Click(object sender, EventArgs e) 
{ 
    //check the file 

    if (FileUpload1.HasFile && FileUpload1.PostedFile != null 
     && FileUpload1.PostedFile.FileName != "") 
    { 
     HttpPostedFile file = FileUpload1.PostedFile; 
     //retrieve the HttpPostedFile object 

     buffer = new byte[file.ContentLength]; 
     int bytesReaded = file.InputStream.Read(buffer, 0, 
          FileUpload1.PostedFile.ContentLength); 

     if (bytesReaded > 0) 
     { 
      try 
      { 
       string connectionString = 
        ConfigurationManager.ConnectionStrings[ 
        "uploadConnectionString"].ConnectionString; 
       connection = new SqlConnection(connectionString); 
       SqlCommand cmd = new SqlCommand 
       ("INSERT INTO Videos (Video, Video_Name, Video_Size)" + 
       " VALUES (@video, @videoName, @videoSize)", connection); 
       cmd.Parameters.Add("@video", 
        SqlDbType.VarBinary, buffer.Length).Value = buffer; 
       cmd.Parameters.Add("@videoName", 
        SqlDbType.NVarChar).Value = FileUpload1.FileName; 
       cmd.Parameters.Add("@videoSize", 
        SqlDbType.BigInt).Value = file.ContentLength; 
       using (connection) 
       { 
        connection.Open(); 
        int i = cmd.ExecuteNonQuery(); 
        Label1.Text = "uploaded, " + i.ToString() + " rows affected"; 
       } 
      } 
      catch (Exception ex) 
      { 
       Label1.Text = ex.Message.ToString(); 
      } 
     } 

    } 
    else 
    { 
     Label1.Text = "Choose a valid video file"; 
    } 
} 
+0

你能從這些鏈接中提取一些有用的數據嗎?這是相當的壓倒性的! – rhughes

+0

@rhughes看到我的答案。 –

1

你說 「應該」 是SQL Server 2008中,但它必須是?

如果不是,SQL Server 2012提供File Tables,專門用於在SQL Server中存儲大型文件。它實際上將數據存儲在文件系統中,但是不需要編寫任何代碼來管理實際文件。

如果確實需要的SQL Server 2008,那麼你有以下幾種選擇:

1)FileStream用VARBINARY(MAX)列類型 - 可以有這種方法的性能問題。我個人不會以這種方式存儲視頻。

2)將文件放在文件系統上(磁盤上),只將文件路徑/文件名存儲在數據庫中。然後編寫代碼來管理文件系統上的CRUD操作。

相關問題