C# App.Config 连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14918912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:26:37  来源:igfitidea点击:

App.Config Connection String

c#winformsdatabase-connectionconnection-string

提问by Amrit Sharma

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

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

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

But its not connected to the database.

但它没有连接到数据库。

Can somebody point out the mistake.

有人能指出错误吗。

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

但是当我在不使用 app.config 的情况下使用此代码时,它工作正常。

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

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

我怎样才能使 app.config 连接字符串工作..

回答by Mohammad Dehghan

It seams (from the comments) that you are targeting two difference database files in those two connection strings. The first one is in your App_Datafolder of your project, and the second one resides on your desktop.

它接缝(从评论中)您的目标是这两个连接字符串中的两个不同数据库文件。第一个位于您App_Data项目的文件夹中,第二个位于您的桌面上。

The file in your App_Datafolder is copied in to the output folder (bin/Debugor bin/Releasefor a WinForms project) every time you start the project in the VS. It overwrites previous contents of the file so every time you have a fresh copy of the file form the App_Datafolder in your output folder. To find out, run the program and execute a few insertions. Then close the program and open the database file in the output folder (not in projects App_Data).

每次在 VS 中启动项目时,App_Data文件夹中的文件都会复制到输出文件夹(bin/Debugbin/ReleaseWinForms 项目)。它会覆盖文件的先前内容,因此每次App_Data您从输出文件夹中的文件夹中获得文件的新副本时。要找出答案,请运行该程序并执行一些插入操作。然后关闭程序并打开输出文件夹中的数据库文件(不在 projects 中App_Data)。

This happens because you have set the Copy to Output Directoryproperty of the database file to Copy always.

发生这种情况是因为您已将Copy to Output Directory数据库文件的属性设置为Copy always.

回答by Mido

You may do it so

你可以这样做

<configuration>
 <appSettings>
   <add key="ApplicationTitle" value="Sample Console Application" />
   <add key="ConnectionString"
       value="Server=localhost;Database=Northwind;Integrated
              Security=false;User Id=sa;Password=;" />
</appSettings>

then use ConfigurationSettings.AppSettings["ConnectionString"];

然后使用 ConfigurationSettings.AppSettings["ConnectionString"];

回答by Sushil Mate

you need to set DataDirectory.

您需要设置数据目录。

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

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

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

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

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

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

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

Hope this helps.

希望这可以帮助。