public bool get_row()
{
if(image_reader.Read())
{
image_filename=image_reader.GetValue(0).ToString();
image_bytes=(byte[])image_reader.GetValue(1);
return true;
}
else
{
return false;
}
}
运行的时候“image_bytes=(byte[])image_reader.GetValue(1);”这句出错,说是指定的转换无效,应该怎么写才能把转换成byte[]格式啊,这行代码是书上的,image_reader是oledb的datareader,帮忙看一下吧,3xs。
{
if(image_reader.Read())
{
image_filename=image_reader.GetValue(0).ToString();
image_bytes=(byte[])image_reader.GetValue(1);
return true;
}
else
{
return false;
}
}
运行的时候“image_bytes=(byte[])image_reader.GetValue(1);”这句出错,说是指定的转换无效,应该怎么写才能把转换成byte[]格式啊,这行代码是书上的,image_reader是oledb的datareader,帮忙看一下吧,3xs。
不知道这样可以是否用Convert类转换.
单个值我试过没用过数组转..如果成功请告诉我.
我就是这么取得阿。
byte[] imagedata=new byte[1];
while(dr.Read ())
{
imagedata=(byte[])dr["pic"]; //有一条记录的datareader
fs.Write (imagedata ,0,imagedata.Length);
}
这是个代码片断将图片存到C盘,供参考
其他信息: 指定的转换无效。”
对了,原来上面这行“image_filename=image_reader.GetValue(0).ToString();”书上也是写成
“image_filename=(string)image_reader.GetValue(0);”也出现上面那个错误,后来改成ToString()后就不出错了,但是下面这行出现了同样的错误,不知怎么回事。新建类的全部代码如下(主要是为了读取数据库中的图片路径):using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;namespace PicLoad_test
{
/// <summary>
/// getdata 的摘要说明。
/// </summary>
public class image_query
{
string image_filename=null;
byte[] image_bytes=null;
OleDbConnection image_connection=null;
OleDbCommand image_command=null;
OleDbDataReader image_reader=null;
public image_query()
{
image_connection=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pic.mdb;");
image_command=new OleDbCommand("SELECT picname,picpath FROM pic",image_connection);
image_connection.Open();
image_reader=image_command.ExecuteReader();
} public Bitmap get_image()
{
MemoryStream ms=new MemoryStream(image_bytes);
Bitmap bmap=new Bitmap(ms);
return bmap;
} public string get_filename()
{
return image_filename;
} public bool get_row()
{
if(image_reader.Read())
{
image_filename=image_reader.GetValue(0).ToString();
image_bytes=(byte[])image_reader.GetValue(1);
return true;
}
else
{
return false;
}
}
public void end_query()
{
image_reader.Close();
image_connection.Close();
}
}
}
我想把数据库里的图片路径读出来赋给picturebox的image属性在窗体上显示图片。