在程序里面我用:private void button1_Click(object sender, System.EventArgs e)//导入数据
{
string str;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Access Files|*.mdb";
openFileDialog1.Title = "请选择一个Access表";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
str =openFileDialog1.FileName;
MessageBox.Show("导入成功!"+str);
if (this.datc.fnGetDataConnection()) 
{
//connection was ok.
          this.dataGrid1.AlternatingBackColor=Color.Wheat;
           this.dataGrid1.ReadOnly=true;
          this.dataGrid1.DataSource=datc.dSet.Tables["one"];

        else 

MessageBox.Show("Connection failed...");
Application.Exit();
}
  }
}
通过STR保存数据库的路径!返回的是用户选择的路径!D:\REPORT\JAC.MDB
然后我的连接类是这样写的:
public class AccessDB
{
public OleDbConnection   con;
public OleDbDataAdapter   dAdapter;
public DataSet   dSet;
---------------主要是下面这段代码-------------------
public string conString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=str";
-------------------------------------------
public bool fnGetDataConnection()
{
try 
{  
    con =new OleDbConnection(conString);     
    dAdapter=new OleDbDataAdapter("select * from one", con);
    dSet=new DataSet(); 
           dAdapter.Fill(dSet,"one");
}
catch(Exception ex) 
{
   MessageBox.Show("Error : "+ex.Message);              return false;
}     return true;
}
}
为什么程序总是提示"找不到文件D:\REPROT\STR",我明明要的是用户取的那个数据库文件!
请问怎样解决?

解决方案 »

  1.   

    public string conString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=str";
    應改為:
    public string conString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str;
    試試!
      

  2.   

    改了,提示str在命名空间或类ACCESSDB.CS中不存在!
    怎么解决啊?
      

  3.   

    trypublic string conString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str;
      

  4.   

    str 是BttonClick的变量,你要传到AccessDB的实例中才可以用啊把你调用AccessDB的代码贴出来
      

  5.   

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    str =openFileDialog1.FileName;
    MessageBox.Show("导入成功!"+str);
    if (this.datc.fnGetDataConnection()) 
    {
    //connection was ok.
    this.dataGrid1.AlternatingBackColor=Color.Wheat;
    this.dataGrid1.ReadOnly=true;
    this.dataGrid1.DataSource=datc.dSet.Tables["one"];
    //invoke the method for DataBinding of TextBoxes
    // fnGetDataBindingForTextBoxes();
    // //set CurrencyManager for the "PersonTable" table
    // fnSetCurrencyManager();
    // //display record numbers in the StatusBar
    // fnDisplayRecordNumbers(); 

    else 

    MessageBox.Show("Connection failed...");
    Application.Exit();
    }
    }
      

  6.   

    private void button1_Click(object sender, System.EventArgs e)//导入数据
    {

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "Access Files|*.mdb";
    openFileDialog1.Title = "请选择一个Access表";
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    str =openFileDialog1.FileName;
    MessageBox.Show("导入成功!"+str);
    if (this.datc.fnGetDataConnection()) 
    {
    //connection was ok.
    this.dataGrid1.AlternatingBackColor=Color.Wheat;
    this.dataGrid1.ReadOnly=true;
    this.dataGrid1.DataSource=datc.dSet.Tables["one"];

    else 

    MessageBox.Show("Connection failed...");
    Application.Exit();
    }
    }
    }
      

  7.   

    在AccessDB 多加一个构造函数,传入路径
    private string str;
    public AccessDB(string DBPath)
    {
    this.str = DBPath;
    }调用的时候用 AccessDB datc = new AccessDB(str);