如何在app.config文件中配置access db文件

本文关键字:文件 配置 access db config app | 更新日期: 2023-09-27 18:28:37

我将access 2010与我的c#项目一起使用,我使用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以在所有计算机中工作而无需更改数据库文件链接吗?

如何在app.config文件中配置access db文件

您可以像以下一样将其添加到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);

//可以用作连接字符串

<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();

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

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;

希望这能有所帮助。