2013-01-24 93 views
0

我在將圖像保存到我的數據庫時遇到問題。 我不知道如何插入或存儲圖像到我的數據庫並顯示在我的GridView中。如何將圖像插入數據庫並在gidview中顯示

這裏是我的我的表的設計:

enter image description here

在我的Web方法:

[WebMethod(EnableSession = true)] 
public string sell_item(string name, Image photo, string description) 
{ 
    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=Bidding;Integrated Security=True"); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("UPDATE login SET name = @name, photo = @photo, description = @description WHERE [email protected]", con); 

    cmd.Parameters.AddWithValue("@name", name); 
    cmd.Parameters.AddWithValue("@photo", photo); 
    cmd.Parameters.AddWithValue("@description", description); 

    cmd.ExecuteNonQuery(); 
    con.Close(); 
    return "Product has been upload successfully!"; 
} 

我的網絡應用程序,調用Web服務代碼:

我使用的FileUpload按鈕選擇我的圖像文件。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    Label1.Text = Convert.ToString(a.sell_item(Convert.ToString(TextBoxName.Text), Convert.ToString(FileUploadPhoto.FileName), Convert.ToString(TextBoxDescription.Text))); 

    Label1.Visible = true; 
    if (Label1.Visible == true) 
    { 
     MessageBox.Show("Item has been uploaded successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); 
     Response.Redirect("Menu page.aspx"); 
    } 
} 

在我的GridView我已經設置的屬性: enter image description here

的圖像將不GridView中顯示。 我還是新來的C#。任何人都可以幫助我?謝謝。

回答

1

如果圖像是不正確保存到數據庫中,儘量節省字節數組:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    if (fileUploadPhoto.HasFile) 
    { 
     byte[] imageBytes = new byte[fileUploadPhoto.PostedFile.InputStream.Length + 1]; 
     fileUploadPhoto.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length); 
    } 

    Label1.Text = Convert.ToString(a.sell_item(Convert.ToString(TextBoxName.Text), imageBytes, Convert.ToString(TextBoxDescription.Text))); 

    Label1.Visible = true; 
    if (Label1.Visible == true) 
    { 
     MessageBox.Show("Item has been uploaded successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); 
     Response.Redirect("Menu page.aspx"); 
    } 
} 

您還需要更新您的照片參數取一個字節數組:

[WebMethod(EnableSession = true)] 
public string sell_item(string name, byte[] photo, string description) 
{ 
    ... 
} 

至於顯示 - 我使用的通用處理器(ashx的)來處理圖像:

public class ImageHandler : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     //Retrieve the image using whatever method and identifier you used 
     byte[] imageData = get_item(context.Request["ID"]); 

     if (imageData.Count > 0) 
     { 
      context.Response.OutputStream.Write(imageData, 0, imageData.Length); 
      context.Response.ContentType = "image/JPEG"; 
     } 
    } 
} 

爲了顯示你可以把圖像中的GridView控件,如果你綁定您的數據源:

<ItemTemplate> 
    <asp:Image ID="Image1" runat="server" 
     ImageUrl='<%# "ImageHandler.ashx?ID=" + Eval("ID")%>'/> 
</ItemTemplate> 

或者,如果你熟悉的jQuery,你可以設置一個佔位符圖像源這樣:

<img id="Image1" /> 

<script type="text/javascript"> 
    $("#Image1").attr("src", "ImageHandler.ashx?ID=" + identifier); 
</script> 

另外還有各種文章中的更多信息:

http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html

0

我不建議將圖像直接存儲在數據庫中,而是將它們存儲在文件系統中並僅保存鏈接。 否則,使用SQLServer 2008使用FILESTREAM屬性。對於小圖像,請使用varbinary類型。

相關問題