user2780851
user2780851

Reputation: 19

How to save video into database using c#?

I have scenario where i want to store video into database and show in grid to download from database ,i will upload video by fileupload control and it should be sql server2008 database,which is best way in c# and asp.net?

Upvotes: 1

Views: 15554

Answers (3)

Ajay
Ajay

Reputation: 6590

There are many link for it. Check out this.

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/

Try this code

 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";
    }
}

Upvotes: 2

JRP
JRP

Reputation: 191

You say it "should" be SQL Server 2008, but does it have to be?

If not, SQL Server 2012 offers File Tables that are designed for storing large files in SQL Server. It actually stores the data on the filesystem but in a way that you don't have to write any code to manage the actual file.

If it does need to be SQL Server 2008 then you have the following options:

1) FileStream using a varbinary(MAX) column type - there can be performance issues with this method. I personally would not store video in this way.

2) Put the file on the file system (on disk) and only store the filepath/filename in the DB. You then write code to manage CRUD operations on the file system.

Upvotes: 1

vino20
vino20

Reputation: 429

It will help you... see the following link http://www.codeproject.com/Articles/308552/Upload-and-Download-Files-to-SQL-Servers-in-ASP-Ne

Upvotes: 0

Related Questions