如何使用C#来保存视频到数据库中? [英] How to save video into database using c#?

查看:223
本文介绍了如何使用C#来保存视频到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况,我想存储视频到网格数据库并显示,从数据库下载,我将通过FileUpload控件上传的视频,它应该是SQL Server2008的数据库,这是在C#和asp.net最好的方法?

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?

推荐答案

有很多链接它。看看这个。

There are many link for it. Check out this.

http://weblogs.asp.net/hajan/archive/2010/06/21/save-and-display-youtube-视频链接上-ASP净website.aspx

http://forums.asp.net/t/1104451.aspx/1?How+to +检索+视频+文件+从+ SQL +服务器+数据库
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净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 /论坛/主题/ 88899 /
HTTP: //www.asp.net/webmatrix/tutorials/10-working-with-video

http://www.c-sharpcorner.com/Forums/Thread/88899/

试试这个代码

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

这篇关于如何使用C#来保存视频到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆