当我单独浏览Datagrid,和单独添加图片时都没有问题,但是当我先添加一张图片到数据库后,再单击datagrid浏览其他行记录时就会出错
我捕捉错误信息提示的是 d:\.....\Data.mdb找不到
我的数据库连接对象用的时相对路径,Data.mdb存在Debug里面,但是他的出错信息不是Data.mdb的原始路径,即他自动改变了默认的路径
这是我添加图片的代码:
private void btnAddPho_Click(object sender, System.EventArgs e)
{
FileStream fs;
string PathImage;
int myposition=this.BindingContext[dsPerson,"person"].Position;
string conStr="Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=Data.Mdb";
OleDbConnection Oleconn=new OleDbConnection(conStr);
Oleconn.Open();
if(DialogResult.OK==this.openFileDialog1.ShowDialog())
{
PathImage=this.openFileDialog1.FileName;
fs=new FileStream(PathImage,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] ib=new byte[fs.Length];
fs.Read(ib,0,ib.Length);
fs.Close();
string imgstr1="Update person set 照片=@img where id="+(myposition+1);
OleDbCommand cmd =new OleDbCommand(imgstr1,Oleconn);
((OleDbParameter)cmd.Parameters.Add("@img",OleDbType.Binary)).Value=ib;
cmd.ExecuteNonQuery();
}
Oleconn.Close();
}
不知道怎么搞,有谁能帮帮我!
我捕捉错误信息提示的是 d:\.....\Data.mdb找不到
我的数据库连接对象用的时相对路径,Data.mdb存在Debug里面,但是他的出错信息不是Data.mdb的原始路径,即他自动改变了默认的路径
这是我添加图片的代码:
private void btnAddPho_Click(object sender, System.EventArgs e)
{
FileStream fs;
string PathImage;
int myposition=this.BindingContext[dsPerson,"person"].Position;
string conStr="Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=Data.Mdb";
OleDbConnection Oleconn=new OleDbConnection(conStr);
Oleconn.Open();
if(DialogResult.OK==this.openFileDialog1.ShowDialog())
{
PathImage=this.openFileDialog1.FileName;
fs=new FileStream(PathImage,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] ib=new byte[fs.Length];
fs.Read(ib,0,ib.Length);
fs.Close();
string imgstr1="Update person set 照片=@img where id="+(myposition+1);
OleDbCommand cmd =new OleDbCommand(imgstr1,Oleconn);
((OleDbParameter)cmd.Parameters.Add("@img",OleDbType.Binary)).Value=ib;
cmd.ExecuteNonQuery();
}
Oleconn.Close();
}
不知道怎么搞,有谁能帮帮我!
image类型实际上将图片转化为流的形式,在以流的形式读取,一般不采用这个,太复杂。
怎么保存当前路径??
能写点代码最好,直观!!!
最后问题肯定是使用openFileDialog时,改变了原先存放Access数据库的相对路径
但是大多解决的不好
谁能帮我彻底解决掉
急!!谢谢!!
解决了!!1楼的其实是对的
但是写的是webform
正解:Data Source="+Application.StartupPath+"\\Data.mdb";结贴!