在C#中将图像上传到数据库

时间:2012-01-09 13:45:57

标签: asp.net file-upload

我在ASP.NET C#中创建了一个应用程序,我想使用ASP:FileUpload工具将图像上传到MySQL(二进制字段)。我只能编码以下内容,无法理解其余部分。我一整天用谷歌搜索,找不到任何相关内容。任何帮助!

ASPX文件

<asp:FileUpload ID="FileUpload1" runat="server" />
br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

C#文件

protected void Button1_Click(object sender, EventArgs e)
    {
        HttpPostedFile fup = FileUpload1.PostedFile;
        cmd = new OdbcCommand("INSERT into profile(picture) VALUES(?)", MyConnection);
        cmd.Parameters.Add("@picture", OdbcType.Binary) = fup;
        MyConnection.Open();
        cmd.ExecuteNonQuery();
        MyConnection.Close();
    }

1 个答案:

答案 0 :(得分:1)

似乎这些行是错误的:

cmd = new OdbcCommand("INSERT into profile(picture) VALUES(?)", MyConnection);
cmd.Parameters.Add("@picture", OdbcType.Binary) = fup;

我想这应该是这样的(或类似的 - 现在无法测试):

cmd = new OdbcCommand("INSERT into profile(picture) VALUES(@picture)", MyConnection);
cmd.Parameters.Add("@picture", OdbcType.Binary).Value = FileUpload1.FileBytes;

另请参阅this similar question