**************************? InsertImage.aspx? ************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using MyORM;
namespace Demos
{
?///
??#region Web 窗体设计器生成的代码
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??}
??#endregion
??private void Button1_Click(object sender, System.EventArgs e)
??{
???int??? intImageSize;
???string strImageType;
???Stream ImageStream;
???????? // Gets the Size of the Image
??????????? intImageSize = PersonImage.PostedFile.ContentLength;
???????? // Gets the Image Type
??????????? strImageType = PersonImage.PostedFile.ContentType;
//????????? Reads the Image
??????????? ImageStream = PersonImage.PostedFile.InputStream;
????????????
???byte[] ImageContent=new byte[intImageSize];
??????????? int intStatus ;
??????????? intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
??????????? //
???#region 1
//????Entity en=Repository.GetEntity("Person");
//???en.SetValue("PersonImage",ImageContent);
//???en.SetValue("PersonImageType",strImageType);
//???Repository.InsertEntity(en);
???#endregion
???//---------
???#region 2
???string SQL="INSERT INTO Person(PersonName,PersonSex,PersonDOB,PersonEmail,PersonImage,PersonImageType) VALUES ( @p_name,@p_sex,@p_birthday,@p_email,@p_img,@p_imgtype)";
???SqlConnection con=Pub.SqlConnection;
???SqlCommand command=new SqlCommand ( SQL,con);
???????????
???SqlParameter param0=new SqlParameter ( "@p_name", SqlDbType.VarChar,50 );
???param0.Value = tb_name.Text;??
???command.Parameters.Add( param0 );???????????
???SqlParameter param1=new SqlParameter ( "@p_sex", SqlDbType.VarChar,20 );??
???param1.Value = this.RadioButtonList1.SelectedItem.Text;
???command.Parameters.Add( param1 );
???????????
???SqlParameter param2 =new SqlParameter ( "@p_birthday", SqlDbType.DateTime);
???param2.Value = tb_birthday.Text;
???command.Parameters.Add( param2 );
???SqlParameter param3=new SqlParameter ( "@p_email", SqlDbType.VarChar,50 );
???param3.Value = tb_email.Text;??
???command.Parameters.Add( param3 );???????????
???SqlParameter param4=new SqlParameter ( "@p_img", SqlDbType.Image );??
???param4.Value = ImageContent;
???command.Parameters.Add( param4 );
???????????
???SqlParameter param5 =new SqlParameter ( "@p_imgtype", SqlDbType.VarChar,50 );
???param5.Value = strImageType;
???command.Parameters.Add( param5 );
???//----------
???try
???{
????con.Open();
????command.ExecuteNonQuery();
????con.Close();
????Response.Write("Insert Sucess.");
???}
???catch(Exception ex)
???{
????Response.Write( ex.ToString());
???}
???#endregion
??}
?}
}
*********************************? ReadImage.aspx*******************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using MyORM;
namespace Demos.从数据库检索图片
{
?///
???try
???{
????myConnection.Open();
????SqlDataReader myDataReader;
????myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
????while(myDataReader.Read())
????{
?????//Response.Clear();
?????Response.ContentType = myDataReader["PersonImageType"].ToString();
?????Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
????}
????myConnection.Close();
???}
???catch (SqlException SQLexc)
???{
????Response.Write(SQLexc.Message);
???}
???????????? //Read();
??}
??public void Read()
??{
???Entity en=Repository.GetEntity("Person",26);
???byte[] b=(byte[])en.GetValue("PersonImage");
???string s=(string)en.GetValue("PersonImageType");
???Response.ContentType=s;
???Response.BinaryWrite(b);
??}
??#region Web 窗体设计器生成的代码
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??}
??#endregion
?}
}
************************************?QueryImage.aspx??*************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace Demos
{
?///
???if(!Page.IsPostBack)
???{
????BindGrid();
????
???}???????????????
???????????
??}
??#region Web 窗体设计器生成的代码
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??}
??#endregion
??
??private void BindGrid()
??{
???string sql="select * from Person";
???SqlConnection myConnection=Pub.SqlConnection;
???SqlCommand myCommand = new SqlCommand(sql, myConnection);
???myCommand.CommandType = CommandType.Text;
???try
???{
????myConnection.Open();
????this.DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
????this.DataGrid1.DataBind();
???}
???catch(SqlException SQLexc)
???{
????Response.Write("提取数据时出现错误:" + SQLexc.ToString());
???}
??}
??protected string FormatURL(object strArgument)
??{
???return "ReadImage.aspx?id=" + strArgument.ToString();
??}
??private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
??{
??}
?}
}