c# 讀取Northwind數(shù)據(jù)庫image字段
更新時(shí)間:2009年03月13日 22:36:36 作者:
我在寫一個(gè)三層結(jié)構(gòu)Demo時(shí),使用了Northwind這個(gè)范例數(shù)據(jù)庫。但是奇怪的是,讀取Categories表的Picture列(image類型)無法在image控件中正常顯示(解決方案在后面代碼中可以看到)。
這里值得一提的是,web控件image不像winForm控件那樣可以通過讀取二進(jìn)制流賦值給image屬性來顯示圖像??梢酝ㄟ^變通的方法來實(shí)現(xiàn),流行的做法是新建一個(gè)頁面專門用來顯示圖像,這里代碼直接用孟子E章前輩的(作了小修改,主要是剔除78個(gè)byte字節(jié)流來正常顯示northwind數(shù)據(jù)庫的圖片):
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可以通過下面方法調(diào)用
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack )
Image1.ImageUrl = FormatURL("1");
}
protected string FormatURL(string strArgument)
{
return "ReadImage.aspx?id=" + strArgument;
}
如果不想新建一個(gè)頁面來承載圖像,也可以使用下面的方法:(注意:下面的類是自定義的,大家看得懂這個(gè)方法就可以了)
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();
}
}
}
ReadImage.aspx.cs
復(fù)制代碼 代碼如下:
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可以通過下面方法調(diào)用
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack )
Image1.ImageUrl = FormatURL("1");
}
protected string FormatURL(string strArgument)
{
return "ReadImage.aspx?id=" + strArgument;
}
如果不想新建一個(gè)頁面來承載圖像,也可以使用下面的方法:(注意:下面的類是自定義的,大家看得懂這個(gè)方法就可以了)
復(fù)制代碼 代碼如下:
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();
}
}
}
相關(guān)文章
asp.net 文件上傳 實(shí)時(shí)進(jìn)度
在swfupload的基礎(chǔ)上增加一些個(gè)性化東西.附圖2張.2009-11-11
利用EF6簡(jiǎn)單實(shí)現(xiàn)多租戶的應(yīng)用
這篇文章主要給大家介紹了關(guān)于如何利用EF6簡(jiǎn)單實(shí)現(xiàn)多租戶應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用EF6具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Asp.net利用JQuery AJAX實(shí)現(xiàn)無刷新評(píng)論思路與代碼
Asp.net利用JQuery AJAX實(shí)現(xiàn)無刷新評(píng)論,此功能是每一個(gè)從事asp.net開發(fā)者的朋友都希望實(shí)現(xiàn)的,本文利用閑暇時(shí)間整理了一些,有需要的朋友可以參考下2012-12-12
.NET Core對(duì)象池的應(yīng)用:設(shè)計(jì)篇
本文主要講解對(duì)象池的三個(gè)核心對(duì)象:表示對(duì)象池的ObjectPool<T>對(duì)象、對(duì)象值提供者的ObjectPoolProvider對(duì)象,已及控制池化對(duì)象創(chuàng)建與釋放行為的IPooledObjectPolicy<T>對(duì)象。感興趣的小伙伴可以參考一下這篇文章2021-09-09
ASP.NET MVC3網(wǎng)站創(chuàng)建與發(fā)布(1)
這篇文章主要介紹了ASP.NET MVC3網(wǎng)站創(chuàng)建與發(fā)布,根據(jù)文章內(nèi)容大家可以實(shí)現(xiàn)發(fā)布網(wǎng)站,感興趣的小伙伴們可以參考一下2015-08-08

