Joseph
Joseph

Reputation: 21

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);

Upvotes: 2

Views: 602

Answers (2)

Joey Phillips
Joey Phillips

Reputation: 1625

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?

Upvotes: 2

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34160

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;

Upvotes: 1

Related Questions