【问题标题】:How to insert image into database and display it in gidview如何将图像插入数据库并在gridview中显示
【发布时间】:2013-01-24 16:14:22
【问题描述】:

我在将图像保存到我的数据库时遇到问题。 我不知道如何将图像插入或存储到我的数据库中并显示在我的网格视图中。

这是我的桌子设计:

在我的网络方法中:

[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 中,我设置了属性:

图像不会显示在网格视图中。 我还是 c# 的新手。任何人都可以帮助我吗?谢谢。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

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

    编辑: This post has some helpful code on storing in an image column.

    【讨论】:

      【解决方案2】:

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

      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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-22
          • 2016-04-29
          • 1970-01-01
          • 2012-05-27
          • 2012-11-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多