一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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字段,轉載需保留來源!

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

主站蜘蛛池模板: 欧美日本在线一区二区三区 | 米奇狠狠干 | 狠狠狠地在啪线香蕉 | 亚洲国产韩国一区二区 | 天天综合网天天综合色 | 热久久国产欧美一区二区精品 | 亚洲黄色在线播放 | 91成人网 | www.天天色| 亚洲国产一区二区三区在线观看 | 久久丝袜精品综合网站 | 欧美另类videosbestsex视频 | 欧美色欧美亚洲另类二区不卡 | 2021久久精品国产99国产 | 中国麻豆 | 亚洲第一香蕉视频 | 中文字幕在线永久在线视频2020 | 天天射天天色天天干 | 天天操天天干天天爽 | 午夜视频在线免费观看 | 91九色视频无限观看免费 | 国产99r视频精品免费观看 | 2021在线永久免费视频 | 激情小说网站 | 国产精品免费一区二区区 | 九九国产在线视频 | 免费国产在线观看不卡 | 久久久噜噜噜久久 | 亚洲国产最新 | 成年网站视频在线观看 | 5g影院天天5g爽天天看 | 国产99er66在线视频 | 另类国产精品一区二区 | 4hc44www四虎永久 | 真实国产乱子伦精品一区二区三区 | 88国产精品视频一区二区三区 | 午夜视频在线观看国产www | 日韩天天摸天天澡天天爽视频 | 日韩精品中文字幕一区二区三区 | 狠狠做久久深爱婷婷97动漫 | 免费观看国产一区二区三区 |