程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> c# 讀取Northwind數據庫image字段

c# 讀取Northwind數據庫image字段

編輯:ASP.NET基礎
這裡值得一提的是,web控件image不像winForm控件那樣可以通過讀取二進制流賦值給image屬性來顯示圖像。可以通過變通的方法來實現,流行的做法是新建一個頁面專門用來顯示圖像,這裡代碼直接用孟子E章前輩的(作了小修改,主要是剔除78個byte字節流來正常顯示northwind數據庫的圖片):
ReadImage.aspx.cs
復制代碼 代碼如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebApplication2
{
public partial class ReadImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strImageID = Request.QueryString["id"];
SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=northwind;User Id=sa;Password=123456;");
SqlCommand myCommand = new SqlCommand("Select Picture from Categories Where CategoryID="
+ strImageID, myConnection);

try
{
myConnection.Open();
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myDataReader.Read())
{
Response.Clear();

Response.ContentType = "image/jpeg";
byte[] b = (byte[])myDataReader["Picture"];
//下面的方法就是用來讓圖片可以正常顯示
byte[] temp=new byte [b.Length -78];
Array.Copy(b, 78, temp, 0, b.Length - 78);
Response.BinaryWrite(temp);
}
myConnection.Close();
}
catch (SqlException SQLexc)
{
Response.Write(SQLexc.ToString ());
}
Response.End();

}
}
}

在源頁面如Default.aspx.cs可以通過下面方法調用
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack )
Image1.ImageUrl = FormatURL("1");

}
protected string FormatURL(string strArgument)
{
return "ReadImage.aspx?id=" + strArgument;
}

如果不想新建一個頁面來承載圖像,也可以使用下面的方法:(注意:下面的類是自定義的,大家看得懂這個方法就可以了)
復制代碼 代碼如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebApplication2
{
public partial class ReadImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strImageID = Request.QueryString["id"];
SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=northwind;User Id=sa;Password=123456;");
SqlCommand myCommand = new SqlCommand("Select Picture from Categories Where CategoryID="
+ strImageID, myConnection);

try
{
myConnection.Open();
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myDataReader.Read())
{
Response.Clear();

Response.ContentType = "image/jpeg";
byte[] b = (byte[])myDataReader["Picture"];
byte[] temp=new byte [b.Length -78];
Array.Copy(b, 78, temp, 0, b.Length - 78);
Response.BinaryWrite(temp);
}
myConnection.Close();
}
catch (SqlException SQLexc)
{
Response.Write(SQLexc.ToString ());
}
Response.End();

}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved