How to use the ConfigurationManager.AppSettings

前端 未结 4 1182
囚心锁ツ
囚心锁ツ 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条回答
  •  长发绾君心
    2021-01-30 21:20

    ConfigurationManager.AppSettings is actually a property, so you need to use square brackets.

    Overall, here's what you need to do:

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

    The problem is that you tried to set con to a string, which is not correct. You have to either pass it to the constructor or set con.ConnectionString property.

提交回复
热议问题