How to use the ConfigurationManager.AppSettings

前端 未结 4 1175
囚心锁ツ
囚心锁ツ 2021-01-30 21:12

I\'ve never used the \"appSettings\" before. How do you configure this in C# to use with a SqlConnection, this is what I use for the \"ConnectionStrings\"

SqlCon         


        
4条回答
  •  梦毁少年i
    2021-01-30 21:33

    Your web.config file should have this structure:

    
        
            
        
    
    

    Then, to create a SQL connection using the connection string named MyConnectionString:

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
    

    If you'd prefer to keep your connection strings in the AppSettings section of your configuration file, it would look like this:

    
        
            
        
    
    

    And then your SqlConnection constructor would look like this:

    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyConnectionString"]);
    

提交回复
热议问题