把sql的数据字段设置成image
把图片传换成流就可以存
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
// System.Drawing.Image insertImage;
// System.IO.Stream insertStream=null;
if(Request.Form["checkresult"].ToString()=="true")
{
string imagetype=null;
string imagesize=null;
if(File1.PostedFile.ContentLength!= 0)
{
System.IO.Stream imageStream = File1.PostedFile.InputStream;
imagetype=File1.PostedFile.ContentType;
imagesize=File1.PostedFile.ContentLength.ToString();
FileByteArray = new System.Byte[File1.PostedFile.ContentLength];
imageStream.Read(FileByteArray,0,File1.PostedFile.ContentLength);
}

SqlTransaction myTrans;
try
{
this.sqlConnection1.Open();

}
catch(Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
myTrans=this.sqlConnection1.BeginTransaction();
try
{
this.sqlCommand1.Transaction=myTrans;
this.sqlCommand1.Parameters["@PersonUnitID_1"].Value=Convert.ToInt32(Session["userUnit"]);
this.sqlCommand1.Parameters["@PersonnelName_2"].Value=name.Text;
this.sqlCommand1.Parameters["@BirthDay_3"].Value=BirthDay.Text;
this.sqlCommand1.Parameters["@Degree_4"].Value=Degree.SelectedValue;;
this.sqlCommand1.Parameters["@JobTime_5"].Value=JobTime.Text;
this.sqlCommand1.Parameters["@PersonnelRank_6"].Value=PersonnelRank.SelectedValue;
this.sqlCommand1.Parameters["@PersonnelDuty_7"].Value=PersonnelDuty.Text;
if(File1.PostedFile.ContentLength!= 0)
{
this.sqlCommand1.Parameters["@PersonnelPhoto_8"].Value=FileByteArray;
this.sqlCommand1.Parameters["@PersonnelPhotoSize_9"].Value=imagesize;
this.sqlCommand1.Parameters["@PersonnelPhotoType_10"].Value=imagetype;
}
else
{
this.sqlCommand1.Parameters["@PersonnelPhoto_8"].Value=DBNull.Value;
this.sqlCommand1.Parameters["@PersonnelPhotoSize_9"].Value=DBNull.Value;
this.sqlCommand1.Parameters["@PersonnelPhotoType_10"].Value=DBNull.Value;
}
this.sqlCommand1.Parameters["@Principal_11"].Value=int.Parse(this.cclass.SelectedValue);
this.sqlCommand1.ExecuteNonQuery();
myTrans.Commit(); }
catch(Exception ex)
{
myTrans.Rollback();
Response.Write(ex.Message);
Response.End();
}
finally
{
this.sqlConnection1.Close();
}
}
}
显示是把流直接输入到网页显示
this.myCommand.CommandText="select PersonnelPhoto,PersonnelPhotoSize,PersonnelPhotoType from Personnel where ID="+MyID.ToString();
myConn.Open();
this.myDataReader=this.myCommand.ExecuteReader();
if(this.myDataReader.Read())
{
Response.ContentType=this.myDataReader["PersonnelPhotoType"].ToString();
Response.OutputStream.Write((byte[])myDataReader["PersonnelPhoto"], 0, int.Parse(myDataReader["PersonnelPhotoSize"].ToString()));
Response.End();
}

this.myDataReader.Close();
this.myConn.Close();