为什么我的代码没有将视频保存在数据库中。 [英] Why my code is not saving the videos in database.

查看:149
本文介绍了为什么我的代码没有将视频保存在数据库中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个代码,用于将视频从网页上传到数据库,但它无法正常工作。每当我选择一个文件并点击上传按钮时,它就说没有选择文件。可能是什么问题。野兔是代码。



我尝试过:



< pre lang =c#> byte [] buffer;
SqlConnection连接;
受保护 void BtnUpload_Click( object sender,EventArgs e)
{
if (FileUpload1.HasFile&& FileUpload1.PostedFile!= null
&& FileUpload1.PostedFile.FileName!=
{
HttpPostedFile file = FileUpload1.PostedFile;
// 检索HttpPostedFile对象
buffer = new byte [file.ContentLength];
int bytesReaded = file.InputStream.Read(buffer, 0
FileUpload1 .PostedFile.ContentLength);
if (bytesReaded > 0
{
try
{
connection = new SqlConnection( 数据源= Khawaja \\SQLEXPRESS;初始目录= dbFiles;集成安全性= True );
SqlCommand cmd = new SqlCommand
INSERT INTO Videos(Video,Video_Name,Video_Size) +
VALUES( @ video,@ videoName,@ videoSize),连接);
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;
使用(连接)
{
connection.Open();
// int i = cmd.ExecuteNonQuery();
cmd.ExecuteReader ();
Label1.Text = 已上传;
}
}
catch (例外情况)
{
Label1.Text = ex.Message。的ToString();
}
最后
{
connection.Close();
}
}
// Response.Redirect(Request.Url.AbsoluteUri) ;
}
else
{
Label1.Text = 选择有效的视频文件;
}
}

解决方案

我看到了一些小问题,但似乎没有什么会导致它失败。



0)当ExecuteReader工作时,ExecuteNonQuery可能会更好。

1)使用语句不按常规方式使用。



但是,根据您的描述,此代码根本没有输入。



请使用调试器逐步查看代码并查看实际发生的情况。


I have write a code to upload the video from the web page to the database but it is not working. whenever i chose a file and click upload button it says no file chosen.what could be the problem . hare is the code.

What I have tried:

byte[] buffer;
    SqlConnection connection;
    protected void BtnUpload_Click(object sender, EventArgs e)
    {
        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
                {
                    connection = new SqlConnection("Data Source=Khawaja\\SQLEXPRESS;Initial Catalog=dbFiles;Integrated Security=True");
                    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();
                        cmd.ExecuteReader();
                        Label1.Text = "uploaded";
                    }
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message.ToString();
                }
                finally
                {
                    connection.Close();
                }
            }
            //Response.Redirect(Request.Url.AbsoluteUri);
        }
        else
        {
            Label1.Text = "Choose a valid video file";
        }
    }

解决方案

I see a couple of minor issues, but nothing that seems like it would cause a failure.

0) While ExecuteReader should work, you might be better served by ExecuteNonQuery.
1) The using statement is not used in the usual way.

However, from what you describe, this code isn't being entered at all.

Please use the debugger to step through the code and see what's actually happening.


这篇关于为什么我的代码没有将视频保存在数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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