Add connectionstring in app.config

喜欢而已 提交于 2019-11-27 22:43:43

问题


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

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

 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

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.


回答1:


If you are running Web project with Class library, add connection string to web.config inside the Web project

If you are running Console/WinForm project with Class library, add connection string to app.config inside the Console/WinForm project

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




回答2:


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.

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.

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




回答3:


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

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



回答4:


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

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.




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/17309797/add-connectionstring-in-app-config

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!