如何向Access的“OLE对象”字段中存取图片?急!

hjk1 2004-08-27 10:06:09
我的图片存入代码如下:
str="insert into CarQingKuang(Photo) values(@photo1)";

OleDbCommand cmd=new OleDbCommand(str,data1.conn());
cmd.Parameters.Add(new OleDbParameter("@photo1",OleDbType.LongVarBinary));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
pictureBox1.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read(myData,0,Convert.ToInt32 (ms.Length ));
cmd.Parameters["@photo1"].Value=myData;
cmd.ExecuteNonQuery();
图片取出代码如下:
string strcmd=String.Format("select Photo from CarQingKuang where CardNum='{0}'",txtCardNum.Text.Trim());
OleDbCommand cmd=new OleDbCommand(strcmd,data1.conn());
OleDbDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
byte[] by=(byte[])dr.GetValue(0);
System.IO.MemoryStream mss = new System.IO.MemoryStream(by);
this.pictureBox1.Image=Image.FromStream(mss);//错误:使用了无效参数
}
运行时系统提示以上错误,急!!请高手帮忙!!分不够再加!
...全文
172 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2004-09-09
  • 打赏
  • 举报
回复
"PhotoData" is equal to "bData".
Knight94 2004-09-09
  • 打赏
  • 举报
回复
Sample:
// Read picture into buffer
byte[] bData=null;
Bitmap picture;
FileStream stream;

// Create a new stream to load this photo into
stream = new FileStream((string)dr["FullPath"],
FileMode.Open,
FileAccess.Read);
// Create a buffer to hold the stream bytes
bData = new byte[stream.Length];
// Read the bytes from this stream
stream.Read(bData, 0, (int)stream.Length);
picture=new Bitmap(stream);
// Now we can close the stream
stream.Close();

//Save it into DB
string strQuery="Insert into yourTable ("
//Fields list
+"PhotoData) "//picture data field
//Fields' value
+"values("
+"@PhotoData)";//Using parameter
OleDbCommand oleComm=new OleDbCommand(strQuery,oleConn);
oleComm.Parameters.Add("@PhotoData",PhotoData);
oleComm.ExecuteNonQuery();
oleComm.Dispose();
cshuiting 2004-09-09
  • 打赏
  • 举报
回复
关注,出现同样问题,不知道怎么解决
Knight94 2004-09-09
  • 打赏
  • 举报
回复
留个邮箱,给你个sample
Knight94 2004-09-09
  • 打赏
  • 举报
回复
当然用过了,上面的代码只是一部分,你需要做相应的改动。
cshuiting 2004-09-09
  • 打赏
  • 举报
回复
愚翁
这段程序你用过了吗?在我这里根本是不能用的
wwonion 2004-08-27
  • 打赏
  • 举报
回复
显然不是这样子的,应该输出数据流!

Response.ContentType="image/pjpeg";
Response.BinaryWrite(by);
hjk1 2004-08-27
  • 打赏
  • 举报
回复
急!!没有人会吗?
张海霖 2004-08-27
  • 打赏
  • 举报
回复
关注,是不是mss==null,测一下
qcny 2004-08-27
  • 打赏
  • 举报
回复
呵呵,顶一下,表示关注

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧