Fetching connection string from appconfig file in c#

二次信任 提交于 2019-11-29 10:21:45

For a non-web project, and with app.config set up as in the OP here's what I usually do, since the config file changes names when the app is compiled (to yourapp.exe.config):

    public static Configuration ExeConfig()
    {
        Assembly service = Assembly.GetAssembly(typeof(YourClass));
        return ConfigurationManager.OpenExeConfiguration(service.Location);
    }

Then to reference the s'th connection string:

ExeConfig().ConnectionStrings.ConnectionStrings[s].ConnectionString
shenhengbin

First, you must add the appSettings tag

 <connectionStrings>
    <appSettings>
      <add name="SqlConnectionString" connectionString="Data Source=xxx.xx.xx.xx;Initial Catalog=xxxxx;User Id=xx;Password=xxx;" providerName="System.Data.SqlClient" />
    </appSettings>
  </connectionStrings>

Second, add a reference to System.Configuration to your project and insert a using System.Configuration in the source.

Then, you can use ConfigurationManager.AppSettings to access your config setting:

string result = ConfigurationSettings.AppSettings["SqlConnectionString"];

Am having the same problem but after much research, i discovered that the reason why the code is throwing a null reference exception is because if you use the connectionString name, it will return null and when you try to call a the ToString() or the ConnectionString property of the ConfigurationManager.ConnectionStrings property that's when the exception is thrown, so to
return the correct connection string, you have to index into the ConnectionStringSettings using the following code.

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[/* index */];

string conString = conSettings.ConnectionString;

I am doing this to get my connect string "OracleDbContext"...

ConfigurationManager.ConnectionStrings["OracleDbContext"].ConnectionString;

my web.config looks like:

  <connectionStrings>
    <add name="OracleDbContext" 
         providerName="Oracle.ManagedDataAccess.Client" 
         connectionString="User Id=oracle_user;              
                           Password=oracle_user_password;
                           Data Source=oracle" />
  </connectionStrings>

One other possible cause of failure could be incorrect name resolution by the .NET framework.

If the application name is longer than the 8.3 name (for instance GreatStuff.exe) it might get launched using its short 8.3 equivalent name (in my example GREATS~1.EXE) and .NET will then look for the app.config file by simply appending .config to the short name. And this will fail.

One thing I've noticed when compiling WinForm App in VS 2013:

I add connection strings my Settings File - which updates my app.config file

When I would expect:

var connectionString = ConfigurationManager.ConnectionStrings["theConnectionStringName"].ConnectionString;

I have to write:

var connectionString = ConfigurationManager.ConnectionStrings["MyAppNameSpace.Properties.Settings.theConnectionStringName"].ConnectionString;

Everything else is business as usual

<configuration>
  <appSettings>
   <add key="ConnectionString" value="blah blah blah"/>  </appSettings>
  </configuration>

Will the above configuration works with the API/code like:

string str = ConfigurationSettings.AppSettings["SqlConnectionString"];

The first syntax is definitely valid.

The possible cause is that the configuration file is not loaded properly in the application. I want to ensure what type of project it is. If it is a WinForm application or Console application, you need set the in the app.config. It will be compiled to .config and loaded when the application is launched. If it is a Web application, I'm afraid you cannot add the node to app.config, since it will not be loaded. You need to put it in the web.config. In addition, if you want to put the settings in the config file with a .net library (DLL), the configuration file will not be loaded anyway. The configuration file will only follow the main application.

Same problem. Based on Merlyn Morgan-Graham and Edward Zhu comments, I rebuilt my project, which used the app.config to create a myapp.exe.config file with the connection string. Original first syntax then worked.

Other problem: Tried using the project properties dialog to modify the connection string and it added "project.My.Mysettings. to the connection string name. Which caused failure.

there is another way to do this for console app:

    <configuration>
  <appSettings>
    <add key="SqlConString" value="some connection string"/>
  </appSettings>

to read it

using System.Configuration;

and then use

string CONSTRING;
AppSettingsReader ap = new AppSettingsReader();
CONSTRING = ap.GetValue("SqlConString",typeof(string)).ToString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!