繁体   English   中英

如何在app.config文件中配置访问数据库文件?

[英]how to configure access db file in app.config file?

我将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");链接到所有形式的数据库文件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文件链接吗?

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

指定的连接字符串正确。

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;

希望这可以帮助。

//可以用作连接字符串

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM