App.Config连接字符串 [英] App.Config Connection String

查看:179
本文介绍了App.Config连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的窗体中,我在app.config中有连接字符串

In my windows form i have connection string in app.config as

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Database"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.accdb"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

在所有类中,我使用以下代码连接到数据库。

And in all classes i am using the following code to connect to the database.

string connString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;

但它未连接到数据库。

可以有人指出错误。

但是当我使用这个代码,而没有使用app.config它工作正常。

But when i use this code without use of app.config it works fine.

   string connString  = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\Amrit\\Desktop\\Database.accdb; Persist Security Info = False;";

如何让app.config连接字符串工作。

How can i make the app.config connection string work..

推荐答案

您需要设置DataDirectory。

you need to set DataDirectory.

然后,您可以在Global.ascx.cs中设置Application_Start中的路径

You can then set the path in Application_Start in your Global.ascx.cs

AppDomain.CurrentDomain.SetData("DataDirectory", "C:\Users\Amrit\Desktop");

http ://stackoverflow.com/a/1409378/2745294

http://stackoverflow.com/a/6708279/2745294

希望这有助。

这篇关于App.Config连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆