往数据库中插入图片的问题

sdrjueuw234 2004-11-21 07:21:43
请教高手,在ASP.NET中怎么样往SQL sever 数据库中插入图片,怎样把数据库中的图片显示在网页上?
...全文
86 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Firestone2003 2004-11-21
  • 打赏
  • 举报
回复
http://dev.csdn.net/article/20/article/21/article/22/22114.shtm
http://www.ccw.com.cn/htm/center/prog/02_8_27_2.asp
有比较详细的介绍

你可以参考一下
Firestone2003 2004-11-21
  • 打赏
  • 举报
回复
把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();

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧