繁体   English   中英

如何将图像插入数据库并在gidview中显示

[英]How to insert image into database and display it in gidview

我在将图像保存到数据库时遇到问题。 我不知道如何将图像插入或存储到数据库中以及如何在gridview中显示。

这是我的桌子设计:

在此处输入图片说明

在我的网络方法中:

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

我在Web应用程序中调用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中,我已经设置了属性: 在此处输入图片说明

该图像不会显示在gridview中。 我还是c#的新手。 有人可以帮助我吗? 谢谢。

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

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

如果图像未正确保存到数据库,请尝试使用字节数组保存:

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

您还需要更新photo参数以采用字节数组:

[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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM