主题:从数据库读不出图片的原因
SqlConnection connstring = new SqlConnection("uid=tg3sql;pwd=tg3sql;Database=tg3_learn;Server=SQLSRV-JPN;Connect Timeout=30");
connstring.Open();
string currentpath = @"F:\sun\test\picture\owerpicture";
string str= this.FileUpload1.PostedFile .FileName;
string con1 = this.FileUpload1.PostedFile.ContentType;//获取文件类型
if (str.LastIndexOf("\\") > -1)
{
str = str.Substring(str.LastIndexOf("\\") + 1);
currentpath = currentpath + @"\" + str;
this.FileUpload1.SaveAs(currentpath);
}
if ( str!="")
{
int length = this.FileUpload1.PostedFile.ContentLength ;
byte[] pic = new byte[length];
this.FileUpload1.PostedFile.InputStream.Read(pic, 0, length);
string sql = "insert into pictest(id,pho,len,con) values ('" +1+ "','" + pic + "','"+length +"','"+con1+"')";
//con1 为上传文件的类型
if (this.connstring .State.ToString() == "Closed")
{
this.connstring .Open();
}
SqlCommand comm = new SqlCommand(sql, connstring);
comm.ExecuteNonQuery();
}
上面为向数据库加载图片:
下面为从数据库读取图片:
connstring.Open();
string sql2 = "select pho,con from pictest where id= '"+1+"' ";
SqlCommand command= new SqlCommand(sql2, this.connstring);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
Response.ContentType =reader["con"].tostring();
Response.BinaryWrite((byte[])reader["pho"]);
}
Response.End();
问题:怎么只数据,但是不显示图片。。。
connstring.Open();
string currentpath = @"F:\sun\test\picture\owerpicture";
string str= this.FileUpload1.PostedFile .FileName;
string con1 = this.FileUpload1.PostedFile.ContentType;//获取文件类型
if (str.LastIndexOf("\\") > -1)
{
str = str.Substring(str.LastIndexOf("\\") + 1);
currentpath = currentpath + @"\" + str;
this.FileUpload1.SaveAs(currentpath);
}
if ( str!="")
{
int length = this.FileUpload1.PostedFile.ContentLength ;
byte[] pic = new byte[length];
this.FileUpload1.PostedFile.InputStream.Read(pic, 0, length);
string sql = "insert into pictest(id,pho,len,con) values ('" +1+ "','" + pic + "','"+length +"','"+con1+"')";
//con1 为上传文件的类型
if (this.connstring .State.ToString() == "Closed")
{
this.connstring .Open();
}
SqlCommand comm = new SqlCommand(sql, connstring);
comm.ExecuteNonQuery();
}
上面为向数据库加载图片:
下面为从数据库读取图片:
connstring.Open();
string sql2 = "select pho,con from pictest where id= '"+1+"' ";
SqlCommand command= new SqlCommand(sql2, this.connstring);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
Response.ContentType =reader["con"].tostring();
Response.BinaryWrite((byte[])reader["pho"]);
}
Response.End();
问题:怎么只数据,但是不显示图片。。。