Can we declare variables in the 'app.config' file?

后端 未结 4 1005
甜味超标
甜味超标 2020-12-16 01:26

I have a form which needs to get connected to SQL Server, and I have a drop down for selecting the list of databases and perform operations like primary key checking, etc.

4条回答
  •  不思量自难忘°
    2020-12-16 02:15

    you can use the connectionStrings tag in the app.config configuration. You can add as many as you want (giving them each a separate key) and then retrieve them

    example app.config xml (set providerName to a valid provider, for example System.Data.SqlClient, and the appropriate connection string) :

    
      
        
          
          
          
        
      
    

    example on getting them and listing them (in your case, you would create the appropriate items in the dropdown and set the values) :

    ConnectionStringSettingsCollection settings =
                ConfigurationManager.ConnectionStrings;
    
            if (settings != null)
            {
                foreach(ConnectionStringSettings cs in settings)
                {
                    Console.WriteLine(cs.Name);
                    Console.WriteLine(cs.ProviderName);
                    Console.WriteLine(cs.ConnectionString);
                }
            }
    

提交回复
热议问题