日期:2014-05-18  浏览次数:20460 次

把图片保存到数据库中,并显示到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