从数据库中读取图片的问题,在线等
int ImgID =11;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connstr"]);
SqlCommand cmd=new SqlCommand();
cmd.CommandText= "SELECT * FROM photoes WHERE photoid="+ImgID;
cmd.Connection=conn;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
Response.ContentType = (string)dr["imagetype"];
this.Response.BinaryWrite((byte[])dr["data"]);
Response.End();
conn.Close();
显示之后没有出现图片
只是System.Byte[]
急,大侠快请!
问题点数:100、回复次数:18Top
1 楼net_lover(【孟子E章】)回复于 2005-07-19 10:19:46 得分 0
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1Top
2 楼boyanyan124(.........)回复于 2005-07-19 10:23:34 得分 0
string strConString = "Persist Security Info=False;UserID=sa;InitialCatalog=CakeShop;Data Source=.";
sqlCon = new SqlConnection(strConString);
string strImg = "select mer_image from merchandise where 条件";
sqlCmd = new SqlCommand(strImg, sqlCon);
sqlCmd.Connection.Open();
string strImage = Convert.ToString(sqlCmd.ExecuteScalar());
sqlCmd.Connection.Close();
Image1.ImageUrl=strImage;Top
3 楼boyanyan124(.........)回复于 2005-07-19 10:25:06 得分 0
mer_image是存放图片的路径,merchandise 是SQL表名Top
4 楼tigerwen01(小虎)回复于 2005-07-19 10:34:11 得分 0
http://www.yesky.com/20030311/1656281.shtmlTop
5 楼ye_zi(行到水穷处·坐看云起时)回复于 2005-07-19 11:16:54 得分 0
孟子的那个可以Top
6 楼wdcszl(Leithon)回复于 2005-07-19 13:44:28 得分 0
不行啊
都试过了
还是只输出
System.Byte[]Top
7 楼boyanyan124(.........)回复于 2005-07-19 14:00:31 得分 0
string strConString = "Persist Security Info=False;UserID=sa;InitialCatalog=CakeShop;Data Source=.";
sqlCon = new SqlConnection(strConString);
string strImg = "select mer_image from merchandise where 条件";
sqlCmd = new SqlCommand(strImg, sqlCon);
sqlCmd.Connection.Open();
string strImage = Convert.ToString(sqlCmd.ExecuteScalar());
sqlCmd.Connection.Close();
Image1.ImageUrl=strImage;
mer_image是存放图片的路径,merchandise 是SQL表名
这可以的,我就是这样读的
Top
8 楼xcz1943(小钊)回复于 2005-07-19 14:07:23 得分 0
我的刚开始也是不能显示图片
那时以为你的数据库链接除了问题
再仔细看看Top
9 楼NetDNASupport(爱因斯坦)回复于 2005-07-19 14:13:56 得分 0
1、建立一个表:
在SQL SERVER中建立这样结构的一个表:
列名 类型 目的
ID Integer 主键ID
IMGTITLE Varchar(50) 图片的标题
IMGTYPE Varchar(50) 图片类型. ASP.NET要以辨认的类型
IMGDATA Image 用于存储二进制数据
2、存储图片到SQL SERVER数据库中
为了能存储到表中,你首先要上传它们到你的WEB 服务器上,你可以开发一个web form,它用来将客户端中TextBox web control中的图片入到你的WEB服务器上来。将你的 encType 属性设置为:myltipart/formdata.
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string imgtitle = TextBox1.Text;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
string connstr=((NameValueCollection)Context.GetConfig("appSettings"))["connstr"];
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand
("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)
VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
SqlParameter paramTitle = new SqlParameter
("@imgtitle", SqlDbType.VarChar,50 );
paramTitle.Value = imgtitle;
command.Parameters.Add( paramTitle);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData );
SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
3、从数据库中恢复读取
现在让我们来从SQL Server中读取我们放入的数据吧!我们将要输出图片到你的浏览器上,你也可以将它存放到你要的位置。
private void Page_Load(object sender, System.EventArgs e)
{
string imgid =Request.QueryString["imgid"];
string connstr=((NameValueCollection)
Context.GetConfig("appSettings"))["connstr"];
string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = " + imgid;
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["imgtype"].ToString();
Response.BinaryWrite( (byte[]) dr["imgdata"] );
}
connection.Close();
}
要注意的是Response.BinaryWrite 而不是Response.Write.
Top
10 楼coley(唉~眼镜又厚了~)回复于 2005-07-19 15:09:10 得分 20
嘿嘿,你这样的情况我遇见过,我肯定的是,你绝对没有把图片存入数据库
贴出你上传图片的代码,让大家给你看看就知道了。要保存byte[]才能有数据Top
11 楼chx_xuxu(逍遥客)回复于 2005-07-19 15:14:19 得分 0
做个记号 呵呵Top
12 楼wdcszl(Leithon)回复于 2005-07-20 22:37:50 得分 0
int imagesize;
string strImageType;
Stream ImageStream;
string ImageName;
int i;
i=this.upload.Value.LastIndexOf(@"\");
ImageName = this.upload.Value.Substring(i+1);
imagesize = upload.PostedFile.ContentLength; // 文件大小
strImageType = upload.PostedFile.ContentType; // 文件类型
ImageStream = upload.PostedFile.InputStream;
byte[] ImageContent = new byte[imagesize];
ImageStream.Read(ImageContent, 0, imagesize);
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["connstr"]);
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
conn.Open();
cmd.CommandText="insert into photoes (data,imagetype,filename,imagesize) values ('"+ImageContent+"','"+strImageType+"','"+ImageName+"','"+imagesize+"')";
cmd.ExecuteNonQuery();
conn.Close();
各大侠帮忙,搞不动了Top
13 楼nmlvjun(网事如风)回复于 2005-07-20 23:13:57 得分 0
http://blog.csdn.net/gaofeng2000/archive/2004/08/27/86264.aspxTop
14 楼wdcszl(Leithon)回复于 2005-07-22 13:45:08 得分 0
coley(唉~眼镜又厚了~) (
我试过了
图片存进去了
代码也帖出来了
但是还是不行啊Top
15 楼sunnystar365(一个人的天空)回复于 2005-07-22 14:27:23 得分 0
我在页面里的DattaGrid放了一个图片,用了一个连接到另一个页面取图片
<asp:Image id=Imagebutton1 runat="server" ImageUrl='<%# "BookCover.aspx?ImageID="+DataBinder.Eval(Container,"DataItem.BookGuid")%>'></asp:Image>
BookCover.aspx
string str=System.Configuration.ConfigurationSettings.AppSettings["cn"];
SqlConnection cn=new SqlConnection(str);
SqlCommand cmd=new SqlCommand();
cmd.CommandText="select Cover from Books where BookGuid='"+this.Request["ImageID"]+"'";
cmd.Connection=cn;
cn.Open();
this.Response.ContentType="image/*";
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.Response.BinaryWrite((byte[])dr["Cover"]);
}
cn.Close();
代码是写在private void Page_Load(object sender, System.EventArgs e)里的
Top
16 楼soft_biao(巴不豆)回复于 2005-07-22 15:53:47 得分 80
图片读取的代码是没有问题,问题就出在图片写入数据库上
下面是帮你修改后的代码
int imagesize;
string strImageType;
Stream ImageStream;
string ImageName;
int i;
i=this.upload.Value.LastIndexOf(@"\");
ImageName = this.upload.Value.Substring(i+1);
imagesize = upload.PostedFile.ContentLength; // 文件大小
strImageType = upload.PostedFile.ContentType; // 文件类型
ImageStream = upload.PostedFile.InputStream;
byte[] ImageContent = new byte[imagesize];
ImageStream.Read(ImageContent, 0, imagesize);
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["connstr"]);
string query="insert into photoes (data,imagetype,filename,imagesize) values (@ImageData,@ImageContentType,"+ImageName+"','"+imagesize+"')";
SqlCommand cmd=new SqlCommand(query,conn);
cmd.Parameters.Add("@ImageData", SqlDbType.Image);
cmd.Parameters.Add("@ImageContentType", SqlDbType.VarChar, 50);
cmd.Parameters["@ImageData"].Value = ImageContent;
cmd.Parameters["@ImageContentType"].Value = strImageTyp;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();Top
17 楼tjdlut(tjdlut)回复于 2005-07-22 16:07:44 得分 0
你就用孟子大哥的链接,
我做了一点问题都没有Top
18 楼jimu8130(火箭的未来在哪里?)回复于 2005-07-22 16:26:10 得分 0
学习ingTop




