【问题标题】:display image from db in image control, alongside other asp.net controls在图像控件中显示来自 db 的图像,以及其他 asp.net 控件
【发布时间】:2014-01-08 16:21:15
【问题描述】:
imageTest table
---------------
imgName ( varchar )
contentType ( varchar )
data ( varbinary(max) )


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <br />
    <asp:Image ID="Image1" runat="server" />

</div>
</form>

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim strCon As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
    Dim con As New SqlConnection(strCon)
    Dim cmd As New SqlCommand()
    cmd.Connection = con
    cmd.CommandType = CommandType.Text

    Dim strSelect As String = "SELECT * FROM [imageTest] WHERE [id] = 1"
    cmd.CommandText = strSelect
    con.Open()
    Dim reader As SqlDataReader = cmd.ExecuteReader() 'must be after con.open()

    If (reader.Read()) Then 'must include for reader

        Label1.Text = reader(0).ToString

        Label2.Text = reader(1).ToString()

        reader.Close()

        con.Close()

    End If

End Sub
End Class



数据库是 SQL Server。图像而不是图像路径在数据库中存储为varbinary(max)

如何显示来自数据库的图像,以及其他控件,例如 labelstextboxes,其内容也从数据库中检索?

我已经看过并尝试了许多教程,但它们要么在 C# 中,要么不起作用,或者向您展示如何仅显示图像,或在 gridview 中显示图像。

欢迎提供示例代码。

【问题讨论】:

    标签: asp.net sql-server database vb.net image


    【解决方案1】:

    首先您必须在我们的解决方案中创建图像文件夹 之后写这个方法

    public static int imageupload(string imagename,string imagepath)
            {
    
            var value = 0;
            try
            {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
            //Open the database connection
            con.Open();
            //Query to insert images path and name into database
            SqlCommand cmd = new SqlCommand("Insert into imagetable(imagename,image) values(@imagename,@image)", con);
            //Passing parameters to query
            cmd.Parameters.AddWithValue("@imagename",imagename);
            cmd.Parameters.AddWithValue("@image", imagepath);
            cmd.ExecuteNonQuery();
            value = 1;
            //Close dbconnection
            con.Close();
             }
            catch(Exception ex)
            {
                throw ex;
            }
            return value;
        }
    

    【讨论】:

      【解决方案2】:
      protected void Button1_Click(object sender, EventArgs e)
          {
              string filename = Path.GetFileName(fileuploadimages1.PostedFile.FileName);
              fileuploadimages1.SaveAs(Server.MapPath("Images/" + filename));
              int a = testapp.imageupload(TextBox1.Text, "Images/" + filename);
      
              if(a==1)
              {
                  Response.Write(@"<script language='javascript'>alert('saved secussfully')</script>");
                  Response.Redirect("home.aspx");
              }
              else
              {
                  Response.Write(@"<script language='javascript'>alert('error whie saving image')</script>");
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-18
        • 2012-09-25
        • 2011-05-18
        • 2012-07-05
        • 1970-01-01
        • 2017-02-18
        相关资源
        最近更新 更多