class MyConnection
{
public OleDbConnection GetConnection()
{
第一种: // string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\\Kucun.mdb";
//正常使用Environment.CurrentDirectory时,会得到当前的文件夹路径,
//但在特殊情况也要注意.如果用另外A程序调用B程序,B程序里的Environment.CurrentDirectory就会是A程序的路径.
//如果用folderBrowserDialog等选择路径组件也会改变Environment.CurrentDirectory,最好用Application.StartupPath代替
下面一种测试过没有错误或许是最优方法:
第二种:string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+ "\\Kucun.mdb";
OleDbConnection con = new OleDbConnection(connectionstring);
return con;
}
}
还有一种不推荐:string s= "Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\data.mdb "谁能详细解释一下这一种连接方式适用于那些连接?
{
public OleDbConnection GetConnection()
{
第一种: // string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\\Kucun.mdb";
//正常使用Environment.CurrentDirectory时,会得到当前的文件夹路径,
//但在特殊情况也要注意.如果用另外A程序调用B程序,B程序里的Environment.CurrentDirectory就会是A程序的路径.
//如果用folderBrowserDialog等选择路径组件也会改变Environment.CurrentDirectory,最好用Application.StartupPath代替
下面一种测试过没有错误或许是最优方法:
第二种:string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+ "\\Kucun.mdb";
OleDbConnection con = new OleDbConnection(connectionstring);
return con;
}
}
还有一种不推荐:string s= "Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\data.mdb "谁能详细解释一下这一种连接方式适用于那些连接?
http://www.cnblogs.com/college/archive/2009/06/09/1499682.html
- For apps running under ClickOnce, this will be a special data folder created by ClickOnce
- For Web apps, this will be the App_Data folder这里说得很清楚了。