<%@ Page Language="C#" %><%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.SqlClient" %><script runat="server">protected void Page_Load( object sender, EventArgs e ){////構建資料庫連接,SQL語句,建立參數//ACCESS資料庫使用本注釋部分//string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");//OleDbConnection myConnection = new OleDbConnection(strCnn);//OleDbCommand command = new OleDbCommand("select * from Person Where PersonID =" + Request.QueryString["id"], myConnection);//myConnection.Open();//OleDbDataReader dr = command.ExecuteReader();//if (dr.Read())//{// Response.Clear();// Response.AddHeader("Content-Type", dr["PersonImageType"].ToString());// Response.BinaryWrite((byte[])dr["PersonImage"]);//}//dr.Close();//myConnection.Dispose();//構建資料庫連接,SQL語句,建立參數string strCnn = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Book;Server=(local);";SqlConnection myConnection = new SqlConnection(strCnn);SqlCommand command = new SqlCommand("select * from UserPhoto Where id =" + Request.QueryString["id"], myConnection);myConnection.Open();SqlDataReader dr = command.ExecuteReader();if (dr.Read()){Response.Clear();Response.AddHeader("Content-Type", dr["ContentType"].ToString());Response.BinaryWrite((byte[])dr["Photo"]);}dr.Close();myConnection.Dispose();}</script>
建立SQL資料表語句
CREATE TABLE [UserPhoto] ([id] [int] IDENTITY (1, 1) NOT NULL ,[UserName] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NOT NULL ,[ContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,[Photo] [image] NOT NULL ,CONSTRAINT [PK_UserPhoto] PRIMARY KEY CLUSTERED([id]) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GO