0

I having problem on saving image into my database. I don't know how to insert or store image into my database and display in my gridview.

Here's my design of my table:

enter image description here

In my web method :

[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 username=@username", 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!";
}

My code in web application which call the web service:

I using FileUpload button to choose my image file.

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");
    }
}

In my gridview I have set the properties : enter image description here

The image wouldn't display in the gridview. I'm still new to c#. Anyone can help me? Thanks.

4

3 回答 3

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";
        }
    }
}

如果您绑定数据源,则可以将图像放在网格视图中进行显示:

<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

于 2013-01-24T17:07:34.230 回答
1

您可能想查看这篇关于如何将图像保存到数据库的文章。有关您尝试保存图像的数据库/列类型的更多信息也会有所帮助。也可能是gridview的代码。

编辑: 这篇文章有一些关于存储在图像列中的有用代码。

于 2013-01-24T16:50:47.417 回答
0

我不建议将图像直接存储在数据库中,而是将它们存储在文件系统中并仅保存链接。否则,在 SQLServer 2008 中使用 FILESTREAM 属性。对于小图像,使用 varbinary 类型。

于 2013-01-24T18:34:23.173 回答