C# 在 app.config 中添加连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17309797/
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 09:07:33  来源:igfitidea点击:

Add connectionstring in app.config

c#class-library

提问by Alma

I have a class library in C# has has several classes and a app.config. I have put ConnectionString in app.config as follows:

我在 C# 中有一个类库有几个类和一个 app.config。我将 ConnectionString 放在 app.config 中,如下所示:

<connectionStrings>
    <add name="TRN_DB"
         connectionString="Database=TRN;Server=NDTSSN;User ID=ln_users2;Password=%4nu$r!;Connection Timeout=30;pooling=false;" 
         providerName="System.Data.SqlClient"
    />
</connectionStrings>

I try to open connection string in one of the classes in my project as follows:

我尝试在我的项目中的一个类中打开连接字符串,如下所示:

     string connectionString = ConfigurationManager.ConnectionStrings["TRN_DB"].ConnectionString.ToString();
       SqlConnection con = new SqlConnection(connectionString);
       con.Open();

But it gave me an error as:

但它给了我一个错误:

{"Object reference not set to an instance of an object."}

{“你调用的对象是空的。”}

The connection string is correct when I use same way in web project inside the web.config, Does anybody know how how I can access it in app.config? I already use it this way too:

当我在 web.config 中的 web 项目中使用相同的方式时,连接字符串是正确的,有人知道我如何在 app.config 中访问它吗?我也已经这样使用它了:

 string connectionString = System.Configuration.ConfigurationSettings.AppSettings["TRN_DB"];

this one give me this error:

这个给了我这个错误:

This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.Appsettings

此方法已过时,已被 System.Configuration!System.Configuration.ConfigurationManager.Appsettings 取代

To make it clear, I don't have any other project in my solution, as I want my class library to work independently. I am not able to app web project and web.config to that. So I either should add connection string to app.config and access it Or add a web.config to my class library project. That I am not sure which one is doable.

明确地说,我的解决方案中没有任何其他项目,因为我希望我的类库独立工作。我无法应用 web 项目和 web.config。因此,我应该将连接字符串添加到 app.config 并访问它,或者将 web.config 添加到我的类库项目中。我不确定哪个是可行的。

采纳答案by Neverever

If you are running Webproject with Class library, add connection string to web.configinside the Webproject

如果你正在运行的Web与类库项目,连接字符串添加到web.config内部网络项目

If you are running Console/WinFormproject with Class library, add connection string to app.configinside the Console/WinFormproject

如果您正在运行控制台/ WinForm的与类库项目,连接字符串添加到app.config内部控制台/ WinForm的项目

modifying config in Library project will not work in your case.

在您的情况下,修改库项目中的配置将不起作用。

回答by Simon Belanger

From what I understand, you have a class library with a configuration file with a connection string and are referencing that library in another app. The problem is an app.config (or web.config) is relative to the executing application.

据我了解,您有一个带有连接字符串的配置文件的类库,并且正在另一个应用程序中引用该库。问题是 app.config(或 web.config)与正在执行的应用程序相关。

Say your class library is named Shared.dll and have a Shared.dll.config and another application (let's say it's a Console Application) called Application with Application.exe.config. When running that application, the app.config used will be Application.exe.config.

假设您的类库名为 Shared.dll,并且有一个 Shared.dll.config 和另一个名为 Application with Application.exe.config 的应用程序(假设它是一个控制台应用程序)。运行该应用程序时,使用的 app.config 将是 Application.exe.config。

If you are trying trying to access a connection string named TRN_DBin this application, it will look in the application's configuration file where it is not defined and return null (hence the Reference Exception).

如果您尝试访问TRN_DB在此应用程序中命名的连接字符串,它将在应用程序的配置文件中查找未定义的位置并返回 null(因此出现引用异常)。

回答by Mike Cheel

The connection string stuff needs to be in the main exe project's app.config or else you need to load it manually.

连接字符串的东西需要在主 exe 项目的 app.config 中,否则你需要手动加载它。

回答by Jason P

As others have said, the library will use the config file for the executing application.

正如其他人所说,库将使用配置文件来执行应用程序。

I'd like to add one suggestion though. One option to consider, for (in my opinion) a bit of polish and a professional touch is creating a custom configuration section. That would allow you to group the settings for your library and not leave any confusion about which settings are used by your library: http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx

不过,我想添加一项建议。一个要考虑的选择,(在我看来)有点润色和专业接触是创建自定义配置部分。这将允许您对库的设置进行分组,并且不会对您的库使用哪些设置造成任何混淆:http: //msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100)。 aspx

回答by Ashraf Abusada

In windows forms applications always add reference to System.Configuration in References then in the code page add using System.Configurationthen use the connection strings like this:

在 Windows 窗体中,应用程序总是在 References 中添加对 System.Configuration 的引用,然后在代码页中添加using System.Configuration然后使用这样的连接字符串:

string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;

回答by Deathstalker

I had a similar problem and the issue turned out to be that I needed to add System.Configuration to the References.

我遇到了类似的问题,结果证明我需要将 System.Configuration 添加到引用中。

I thought adding using System.Configuration; to the class would do the trick but I had to add System.Configuration to the references before it would work.

我想添加使用 System.Configuration; 到类可以解决问题,但我必须在它起作用之前将 System.Configuration 添加到引用中。