2011-03-08 27 views
1

我使用下面的代碼從我的SQL Server流式傳輸.flv視頻。但根據我的理解,整個視頻在播放之前會被加載到內存中。我想要做的是將CommandBehavior.SequentialAccess添加到SQLDataReader中,但我無法使其工作。如何添加對我的視頻流的順序訪問?

請幫我這裏。如果你能提供一個適用於我的工作示例,我會很高興。僞代碼將是第二好的選擇。任何指針雖然讚賞。

這裏是我的HttpHandler

public class ViewFilm : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     try 
     { 
      // Check if video id was given 
      if (context.Request.QueryString["id"] != null) 
      { 
       string video_ID = context.Request.QueryString["id"]; 

       // Connect to DB and get the video 
       using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString)) 
       using (SqlCommand cmd = new SqlCommand("GetVideo", con)) 
       { 
        cmd.CommandType = CommandType.StoredProcedure; 
        SqlParameter sqlParam = cmd.Parameters.Add("@videoId", SqlDbType.Int); 
        sqlParam.Value = video_ID; 

        con.Open(); 
        using (SqlDataReader dr = cmd.ExecuteReader()) 
        { 
         if (dr.HasRows) 
         { 
          dr.Read(); 
          // Add HTTP header: cache, content type and length 
          context.Response.Cache.SetCacheability(HttpCacheability.Public); 
          context.Response.Cache.SetLastModified(DateTime.Now); 
          context.Response.AppendHeader("Content-Type", "video/x-flv"); 
          context.Response.AppendHeader("Content-Length", ((byte[])dr["data"]).Length.ToString()); 
          context.Response.BinaryWrite((byte[])dr["data"]); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.ToString()); 
     } 
    } 

    public bool IsReusable 
    { 
     get { return false; } 
    } 
} 
+1

我不認爲從數據庫流視頻是一個好主意 - 重新考慮你的設計 – BrokenGlass 2011-03-08 15:38:56

+0

剛剛離開課堂,並刪除了我的評論。我的錯。 – Niklas 2011-03-09 12:39:05

回答

0

第一:不要從數據庫流。第二:如果你真的想要從數據庫流式傳輸,here就是你如何做的例子。但是不要。真。將指針(文件路徑)存儲在數據庫中,並將流處理留給ISS或您使用的任何服務器。

+0

謝謝!這是我正在尋找的。 – Niklas 2011-03-09 08:40:12