四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

c# 讀取Northwind數據庫image字段

這里值得一提的是,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();

}
}
}

AspNet技術c# 讀取Northwind數據庫image字段,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 苏教版二年级下册数学| 地下车库设计规范| 光明力量2古代封印攻略| 清白堂记| 饥渴少妇av| 777精品| 在线播放国内自拍情侣酒店| 正在行动| 全国精神病查询系统官网| 黄视频免费看网站| 苦菜花电视剧| 菲律宾电影毕业生代表| 打美女光屁股视频网站| 工作细胞 2018| 回收名表价格查询| 风云太白山电影| 浙江卫视是几台| 大园桃子| 皮皮电影| 色即是空韩国| infrustructure| 少妇性按摩bbwzⅹxoo| 龙的故乡 电影| 卜冠今| 我妻子的一切 电影| 叶子楣图片| 白世莉电影| 欧若拉歌词| 电影交换| 查理和劳拉| 相声剧本(适合学生)| 749局啥时候上映| 变形金刚2演员表| 热带雨林电影完整版播放| 黄视频在线网站| 色戒在线视频观看| 美食总动员在线观看完整版免费| lilywei| 学校急招水电工一名| 都市频道在线直播观看| 情欲狂欢h版|