如何在app.config文件中配置访问db文件?

时间:2013-08-21 08:39:08

标签: c# ms-access-2010

我使用我的c#项目访问2010我使用OleDbConnection db_con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\projecty bashi nawxoyy\Bashi Nawxoyy\Bashi Nawxoyy\db_file.accdb");链接到我所有表单中的数据库文件知道我想在我的app.config文件中配置OleDbConnection以在所有机器中工作而无需更改db文件链路?

3 个答案:

答案 0 :(得分:0)

你可以在app.config中添加它,就像这样

<connectionStrings>
  <add connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\projecty bashi nawxoyy\Bashi Nawxoyy\Bashi Nawxoyy\db_file.accdb" name="connectionString"/>
</connectionStrings>

并像这样使用

string conString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString();
db_con = new OleDbConnection(conString);

答案 1 :(得分:0)

指定的连接字符串是正确的。

app.config

<add connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
     Data Source=complete path of accdb" name="Connectionstring"/>

您将在C#中将其称为:

OleDbConnection dbConnection = new OleDbConnection();

dbConnection.ConnectionString =
    ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;

希望这有帮助。

答案 2 :(得分:0)

//这可以用作连接字符串

<connectionStrings>
                       <add connectionString="Data Source=Test;D:\projecty bashi nawxoyy\Bashi Nawxoyy\Bashi Nawxoyy\db_file.accdb Catalog=Demo; User ID=sa; Password=Password" name="DemoConnection" providerName="Microsoft.ACE.OLEDB.12.0" />

// And You can use this in constructor of the class file as bellow
 string connStringKey = "DemoConnection";
          connectionString = ConfigurationManager.ConnectionStrings[connStringKey].ToString();