网上也看过很多例子 但是就是写的不对 大家看代码如下
private void button2_Click(object sender, System.EventArgs e)
{


OleDbConnection OleConn=new OleDbConnection();
OleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath + @"E:\\db1.mdb";
OleConn.Open();
System.IO.FileStream fs=new System.IO.FileStream("f:\\1-2.GIF",System.IO.FileMode.Open ,System.IO.FileAccess.Read );
 byte[] ib=new byte[fs.Length];
 fs.Read(ib,0,ib.Length);
 fs.Close();
     
 OleDbCommand cmd = new OleDbCommand("INSERT INTO [image](image1) VALUES (@img )" ,OleConn);
 ((OleDbParameter)cmd.Parameters.Add( "@img" , OleDbType.Binary )).Value = ib;
 cmd.ExecuteNonQuery();
 OleConn.Close();
 MessageBox.Show("写入成功!");  
}
总是说未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。
看来是
System.IO.FileStream fs=new System.IO.FileStream("f:\\1-2.GIF",System.IO.FileMode.Open ,System.IO.FileAccess.Read );
出了问题
可我已经在命民空间加了 
using System.Data;
using System.Data.OleDb;
using System.IO;
了 还能哪里出错呢 百思不得其解 求高手给出代码和解释 我都快崩溃了

解决方案 »

  1.   

    + Application.StartupPath + @"E:\\db1.mdb";
    这样写合理吗???肯定是没有找到数据库文件
      

  2.   

    OleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +  @"E:\\db1.mdb";
      

  3.   

    很可能是因为你的程序没有写入那个 Access 数据库的权限。把Access数据库的权限和他所在目录的权限的EveryOne用户的权限设为最大试试。
      

  4.   

    OleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +  @"E:\\db1.mdb";

    OleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +  @"\\db1.mdb";
    把db1.mdb文件放到你的程序目录下
      

  5.   

    谢谢以上诸位 我把
    OleDbConnection OleConn=new OleDbConnection();
    OleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath + @"E:\\db1.mdb";
    改成
    OleDbConnection OleConn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb");

    问题就奇迹般解决了
    不过还有一个小问题
    我想打开文件 然后选择一幅图 
    if (this.openFileDialog1.FileName!="") //{
     
     //FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
     //string PathImage=this.openFileDialog1.FileName;
     FileStream fs=new FileStream("f:\\1-2.GIF",System.IO.FileMode.Open ,System.IO.FileAccess.Read );
     byte[] ib=new byte[fs.Length];
     fs.Read(ib,0,ib.Length);
     fs.Close();
         //this.PathImage=this.openFileDialog1.FileName;  OleDbCommand cmd = new OleDbCommand("INSERT INTO [Image1](Image1) VALUES (@img )" ,OleConn);
     ((OleDbParameter)cmd.Parameters.Add( "@img" , OleDbType.Binary )).Value = ib;
     cmd.ExecuteNonQuery();
     OleConn.Close();

    MessageBox.Show("写入成功!");  
    }但系统提示我 嵌入的语句不能是声明或标记语句
    也就是
    FileStream fs=new FileStream("f:\\1-2.GIF",System.IO.FileMode.Open ,System.IO.FileAccess.Read );
    出了问题 
    但我看很多例子上都是这样用的 不知道为什么错 怎么改呢
    改成我打开文件框 选择一个图片存入acess的情况
      

  6.   

    你在连接字符串中多加了E:,如果你的程序在D:\test\debug下,那么,你的连接字符串就是
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:\test\debugE:\db1.mdb
    你仔细看看这样对吗?
      

  7.   

    我彻底搞定了 把代码贴出来共享:
    private void button2_Click(object sender, System.EventArgs e)
    {

    FileStream fs;
    string PathImage;
    OleDbConnection OleConn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb");
    OleConn.Open();
    //if (this.openFileDialog1.FileName!="" )
    if (DialogResult.OK == this.openFileDialog1.ShowDialog()) {
     
    //FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
    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();
    //this.PathImage=this.openFileDialog1.FileName; OleDbCommand cmd = new OleDbCommand("INSERT INTO [Image1](Image1) VALUES (@img )" ,OleConn);
    ((OleDbParameter)cmd.Parameters.Add( "@img" , OleDbType.Binary )).Value = ib;
    cmd.ExecuteNonQuery();
    OleConn.Close();

    MessageBox.Show("写入成功!"); }
    }
      

  8.   

    OleDbConnection OleConn=new OleDbConnection();
    OleConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Application.StartupPath + @"E:\\db1.mdb";
    改成
    OleDbConnection OleConn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb");

    问题就奇迹般解决了