色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

c# 讀取Northwind數(shù)據(jù)庫(kù)image字段

這里值得一提的是,web控件image不像winForm控件那樣可以通過(guò)讀取二進(jìn)制流賦值給image屬性來(lái)顯示圖像。可以通過(guò)變通的方法來(lái)實(shí)現(xiàn),流行的做法是新建一個(gè)頁(yè)面專(zhuān)門(mén)用來(lái)顯示圖像,這里代碼直接用孟子E章前輩的(作了小修改,主要是剔除78個(gè)byte字節(jié)流來(lái)正常顯示northwind數(shù)據(jù)庫(kù)的圖片):
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"];
//下面的方法就是用來(lái)讓圖片可以正常顯示
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();

}
}
}

在源頁(yè)面如Default.ASPx.cs可以通過(guò)下面方法調(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è)頁(yè)面來(lái)承載圖像,也可以使用下面的方法:(注意:下面的類(lèi)是自定義的,大家看得懂這個(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();

}
}
}

AspNet技術(shù)c# 讀取Northwind數(shù)據(jù)庫(kù)image字段,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 台安县| 大姚县| 阜新| 南陵县| 合川市| 太原市| 天峨县| 阿图什市| 三原县| 伊宁市| 阳谷县| 乌拉特后旗| 舞钢市| 彝良县| 密山市| 肥城市| 璧山县| 太保市| 喀喇沁旗| 奎屯市| 新营市| 海兴县| 民勤县| 东港市| 印江| 贞丰县| 开封市| 泗水县| 双江| 萍乡市| 朝阳市| 乐业县| 拜泉县| 福泉市| 左权县| 济源市| 许昌县| 玉环县| 凤台县| 灵璧县| 衡水市|