Fetching connection string from appconfig file in c#

前端 未结 10 885
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 07:39

I have the following connection string declared in my app.config file:

  
    

        
相关标签:
10条回答
  • 2020-12-19 07:52

    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.

    0 讨论(0)
  • 2020-12-19 08:02

    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;
    
    0 讨论(0)
  • 2020-12-19 08:03

    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
    
    0 讨论(0)
  • 2020-12-19 08:03

    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>
    
    0 讨论(0)
  • 2020-12-19 08:03

    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();
    
    0 讨论(0)
  • 2020-12-19 08:04

    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.

    0 讨论(0)
提交回复
热议问题