2012-01-09 94 views
0

我在ASP.NET C#中創建了一個應用程序,並且想要使用ASP:FileUpload工具將圖像上載到MySQL(二進制字段)。我只能編寫以下代碼,而其他代碼則無法理解。我一整天都在Google上搜索,找不到任何相關信息。任何幫助!在C#中將圖像上傳到數據庫中

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(); 
    } 
+0

[插入圖像到mysql數據庫使用fileupload控件]可能的重複(http://stackoverflow.com/questions/5724505/inserting-an-image-into-mysql-database-using-fileupload-control) – M4N 2012-01-09 14:10:35

回答

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

+0

OP還需要將OdbcCommand更新爲「插入配置文件(圖片)VALUES(@picture)」 – 2012-01-09 14:21:22

+0

謝謝@布萊恩。我沒有注意到這一點。 – M4N 2012-01-09 14:30:53

相關問題