简体   繁体   中英

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.

//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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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