加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#连接字符串和连接app.config

发布时间:2020-12-16 01:27:01 所属栏目:百科 来源:网络整理
导读:我有以下连接字符串,我通过它连接到位于我的应用程序的根文件夹内的子文件夹中的Access数据库(.mdb): OleDbConnection con = new OledbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory() + "
我有以下连接字符串,我通过它连接到位于我的应用程序的根文件夹内的子文件夹中的Access数据库(.mdb):

OleDbConnection con = new OledbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory() + "Datarctts.mdb;Jet OLEDB:Database Password=mypassword;")

我的问题是,如何将连接字符串放在app.config文件中?一般来说,我使用:

<connectionStrings>
    <add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

我面临的问题是,在我的连接字符串中,我正在进行一些连接,并且还使用System.AppDomain.CurrentDomain.BaseDirectory()来设置数据源…
我如何在app.config中进行相同的连接?有可能这样做吗?

解决方法

您可以尝试以下方法:

connectionString="Data Source=|DataDirectory|rctts.mdb;Initial Catalog=OmidPayamak;Integrated Security=True"

然后尝试将DataDirectory的值设置如下:

var currentDomain = AppDomain.CurrentDomain;
var basePath = currentDomain.BaseDirectory;
currentDomain.SetData("DataDirectory",basePath+"Data");

在您的应用程序的相应启动文件中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读