读书人

把图片保存到数据库中并显示到GridV

发布时间: 2012-03-13 11:21:12 作者: rapoo

把图片保存到数据库中,并显示到GridView中
那位大侠帮助指点一下。
string fullpath = FileUpload1.PostedFile.FileName;
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//把所选图片文件的流中的数据读入字节数组

SQL = "Insert into XXXX_Image (PsnImage) Values ('" +imagebytes + "') ";



[解决办法]
private string ConvertBytesToString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
string sby = Convert.ToString(b, 16);

if (sby.Length == 1)
{
sby = "0" + sby;
}
sb.Append(sby);
}
return sb.ToString();
}



string fullpath = FileUpload1.PostedFile.FileName;
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//把所选图片文件的流中的数据读入字节数组

SQL = "Insert into XXXX_Image (PsnImage) Values (0x" + ConvertBytesToString(imagebytes) + ") ";


至于显示,参考以下链接:
http://blog.csdn.net/lsd123/article/details/3655370

读书人网 >asp.net

热点推荐