如何在Visual studio 2008中使用C#.net上传数据库中的视频

本文关键字:net 的视频 数据库 Visual studio 2008 | 更新日期: 2023-09-27 18:08:09

我有下面的代码将视频文件上传到数据库中,但当我们上传视频并点击上传按钮时,我的网页就会离线,数据不会存储到数据库中。我不知道发生了什么。

我的数据库表是

CREATE TABLE [dbo].[tblVideos](
[id] [int] IDENTITY(1,1) NOT NULL,
[Video_Name] [varbinary](50) NULL,
[Name] [varchar](50) NULL,
[ContentType] [varbinary](50) NULL,
[Data] [varbinary](max) NULL
) ON [PRIMARY]

我的aspx代码

<table style="margin-left:150px; height:546px">
      <tr class="tr">
          <td colspan="3">
              <asp:Label ID="Label1" runat="server" 
                  Font-Size="15pt" ForeColor="#0099FF" Text="Add New Video"></asp:Label>
              &nbsp;<br />
              <asp:Label ID="Label2" runat="server" Font-Names="MS Reference Sans Serif" 
                Font-Size="7pt" Text="Make Sure Video in MP4 format"></asp:Label>
          </td>
      </tr>
      <tr class="tr">
       <td class="style1"> Video Name&nbsp; :</td>
       <td><asp:TextBox ID="txtideoname"  Height="24px" Width="270px" runat="server"></asp:TextBox></td>
        <td>
          &nbsp;</td>
      </tr>
      <tr class="tr">
       <td class="style1">Video type :</td>
       <td><asp:TextBox ID="txtvideotype"  Height="24px" Width="270px" runat="server"></asp:TextBox></td>
        <td>
          &nbsp;</td>
      </tr>
      <tr class="tr">
       <td  class="style1">video:</td>
       <td>
         <asp:FileUpload ID="FileUpload1" runat="server" /></td>
        <td>
          &nbsp;</td>
      </tr>
      <tr class="tr">
       <td>&nbsp;</td>
       <td><asp:Button ID="btnupload" runat="server" Text="Upload" Width="132px" Height="34px" BackColor="Red" BorderStyle="None" Font-Bold="True" ForeColor="White" onclick="btnupload_Click"/><br />
           <asp:Label ID="lblMsg" runat="server"/></td>
        <td>
          &nbsp;</td>
      </tr>
      <tr class="tr">
          <td>
              &nbsp;</td>
          <td>
              &nbsp;</td>
          <td>
            &nbsp;</td>
      </tr>
  </table>

my C# Code
 using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
        {
            byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
            string strConnString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "insert into tblVideos(Video_Name,Name, ContentType, Data) values (@Video_Name,@Name, @ContentType, @Data)";
                    cmd.Parameters.AddWithValue("@Video_Name", txtideoname.Text);
                    cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
                    cmd.Parameters.AddWithValue("@ContentType", "video/mp4");
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }

如何在Visual studio 2008中使用C#.net上传数据库中的视频

一个潜在的问题是Video_NameContentType也被存储为varbinary,并且在SQL中似乎没有强制转换。你试过把它们改成varchar吗?