在程序里面我用: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",我明明要的是用户取的那个数据库文件!
请问怎样解决?
{
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",我明明要的是用户取的那个数据库文件!
请问怎样解决?
應改為:
public string conString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str;
試試!
怎么解决啊?
{
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();
}
}
{
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();
}
}
}
private string str;
public AccessDB(string DBPath)
{
this.str = DBPath;
}调用的时候用 AccessDB datc = new AccessDB(str);