2013-12-19 62 views
0
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> 

显示图像,与其他asp.net控制

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中显示图像。

样本代码是最受欢迎的。

回答

0

首先,你必须在我们的解决方案 创建图像文件夹令状后,这种方法

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

    }