如何使用ConfigurationManager.AppSettings [英] How to use the ConfigurationManager.AppSettings
问题描述
我以前从未使用过"appSettings".您如何在C#中将此配置为与SqlConnection一起使用,这就是我用于"ConnectionStrings"的方式
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"
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
这就是我需要的"appSettings"
And this is what I have for the "appSettings"
SqlConnection con = new SqlConnection();
con = ConfigurationManager.AppSettings("ConnectionString");
但它不起作用.
推荐答案
ConfigurationManager.AppSettings
实际上是一个属性,因此您需要使用方括号.
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"]);
问题是您试图将con设置为字符串,这是不正确的.您必须将其传递给构造函数或设置con.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.
这篇关于如何使用ConfigurationManager.AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!