图片二进制存入数据库 求助 我想把图片编程二进制 存入数据库 以及读取 请问这个代码怎么写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写数据,数据库中图片ID自增, int intImageSize; string strImageType; Stream ImageStream; intImageSize = this.FileUpload1.PostedFile.ContentLength; strImageType = this.FileUpload1.PostedFile.ContentType; ImageStream = this.FileUpload1.PostedFile.InputStream; byte[] imagecontent = new byte[intImageSize]; int n = ImageStream.Read(imagecontent, 0, intImageSize); con.open()//打开数据库联接 sqlcommand cmd=new sqlcommand("INSERT INTO Image(imgtype , imgdata) valueS ( "+ strImageType +", "+ imagecontent +")",con ); ); cmd.ExecuteNonQuery();读数据Private void Page_Load(object sender, System.EventArgs e){ string imgid =Request.QueryString["id"];// 取图片IDstring sql="SELECT imgdata, imgtype FROM Image WHERE id = " + imgid; SqlCommand command = new SqlCommand(sql, con); con.Open();SqlDataReader dr = command.ExecuteReader(); if(dr.Read()){ Response.ContentType = dr["imgtype"].ToString(); Response.BinaryWrite( (byte[]) dr["imgdata"] ); } connection.Close(); } //存图片这个是SQL的scom=new SqlCommand("insert into Photo values('"+ID+"',@ph)",scon);FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read);byte []bt=new byte[fs.Length]; fs.Read(bt,0,bt.Length);fs.Close();SqlParameter Spr=new SqlParameter("@ph",SqlDbType.Image,bt.Length,ParameterDirection.Input, false,0,0,null,DataRowVersion.Current,bt);scom.Parameters.Add(Spr);scom.ExecuteNonQuery();//取图片SqlCommand scom1=new SqlCommand();scom1.Connection=scon;scom1.CommandType=CommandType.Text;scom1.CommandText="select Photo from Photo where Fid="+bh+"";phdr=scom1.ExecuteReader();phdr.Read();MemoryStream buf=new MemoryStream((byte[])phdr[0]);Image image=Image.FromStream(buf,true);this.pictureBox1.Image=image; //显示到pictureBox控件中phdr.Close(); 给你个例子图片保存到数据库和从数据库读取图片并显示(C#) http://www.cnblogs.com/tuyile006/archive/2007/01/08/614718.html 给WPF中的LISTVIEW增加ITEM 如何解决对treeview节点的删除也发生在磁盘的物理文件上 关于计算日期差并给与提示的问题!急!!!!!! C#运行access数据库中加入新行中出现的错误,高手帮我解答一下吧,谢谢 求datagrid删除选中多行的问题 如何分析IQueryable中查询到的表和字段 VS2008下遇到的LINQ子查询问题 linq语句问题 ListView控件的ListItem的高度怎样设置的?怎样让列的长度不可变? 受管理的C++ 为什么错误?? 如何传值 修改listBOX选中项的值?
int intImageSize;
string strImageType;
Stream ImageStream;
intImageSize = this.FileUpload1.PostedFile.ContentLength;
strImageType = this.FileUpload1.PostedFile.ContentType;
ImageStream = this.FileUpload1.PostedFile.InputStream;
byte[] imagecontent = new byte[intImageSize];
int n = ImageStream.Read(imagecontent, 0, intImageSize); con.open()//打开数据库联接
sqlcommand cmd=new sqlcommand("INSERT INTO Image(imgtype , imgdata)
valueS ( "+ strImageType +", "+ imagecontent +")",con );
);
cmd.ExecuteNonQuery();读数据
Private void Page_Load(object sender, System.EventArgs e)
{
string imgid =Request.QueryString["id"];// 取图片ID
string sql="SELECT imgdata, imgtype FROM Image WHERE id = " + imgid;
SqlCommand command = new SqlCommand(sql, con);
con.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["imgtype"].ToString();
Response.BinaryWrite( (byte[]) dr["imgdata"] ); }
connection.Close(); }
scom=new SqlCommand("insert into Photo values('"+ID+"',@ph)",scon);
FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read);
byte []bt=new byte[fs.Length];
fs.Read(bt,0,bt.Length);
fs.Close();
SqlParameter Spr=new SqlParameter("@ph",SqlDbType.Image,bt.Length,ParameterDirection.Input,
false,0,0,null,DataRowVersion.Current,bt);
scom.Parameters.Add(Spr);
scom.ExecuteNonQuery();
//取图片
SqlCommand scom1=new SqlCommand();
scom1.Connection=scon;
scom1.CommandType=CommandType.Text;
scom1.CommandText="select Photo from Photo where Fid="+bh+"";
phdr=scom1.ExecuteReader();
phdr.Read();
MemoryStream buf=new MemoryStream((byte[])phdr[0]);
Image image=Image.FromStream(buf,true);
this.pictureBox1.Image=image; //显示到pictureBox控件中
phdr.Close();