简体   繁体   English

如何使用 C# 在 Sql-Server 中插入视频?

[英]How to Insert Videos in Sql-Server using C#?

When I try to save a video in Sql server, i'm facing to this kind of error bellow: The request filtering module is configured to deny a request that exceeds the request content length.当我尝试在 Sql 服务器中保存视频时,我面临以下这种错误:请求过滤模块被配置为拒绝超过请求内容长度的请求。

//Converting a file Uploaded in byte before inserting it in DB.

using (BinaryReader br = new BinaryReader(fs))
{
   byte[] bytes = br.ReadBytes((Int32)fs.Length);
   string constr = (@"Data Source=(localdb)\MSSQLLocalDB;....");               

   using (SqlConnection con = new SqlConnection(constr))
   {
      string query = "insert into tblFiles values (@Name, @ContentType, @Data)";

      using (SqlCommand cmd = new SqlCommand(query))
      {
         //// ????
      }
   }
}

//The Kind of table i'm Using in Sql_Server //我在Sql_Server中使用的表类型

CREATE TABLE tblFiles(Id int IDENTITY PRIMARY KEY,Name 
           VARCHAR(100) NOT NULL,
            ContentType NVARCHAR(4000)NOT NULL, Data VARBINARY(MAX)NOT 
              NULL);

just add it with parameters like any other datatypes:只需像任何其他数据类型一样添加参数即可:

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = name;
cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes;
cmd.Parameters.Add("@ContentType", SqlDbType.NVarChar).Value = contentType;

Rather then trying to codify the video data into bytes and store it in the database it would be better practice to store the video on your disk and store the path to the video in your database.与其尝试将视频数据编码为字节并将其存储在数据库中,不如将视频存储在磁盘上并将视频的路径存储在数据库中。 Thus rather then bloating your database you have more efficient design.因此,而不是膨胀你的数据库,你有更有效的设计。

Storing Images in DB - Yea or Nay? 在数据库中存储图像 - 是还是不是?

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

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