相关问题在网络上只是通过ConfigurationManager.AppSettings来读取appSettings中add节点属性的。
我呢,想读取Visual Studio自动生成的数据库连接字符串。这个字符串是记录在connectionString节点中的。
配置文件如下:
代码
1 <?xml version="1.0"?>
2 <configuration>
3 <connectionStrings>
4 <add name="HairCuting.Properties.Settings.DataBaseConnectionString"
5 connectionString="Data Source=|DataDirectory|\DataBase.sdf"
6 providerName="Microsoft.SqlServerCe.Client.3.5" />
7 </connectionStrings>
8 <startup>
9 <supportedRuntime version="v2.0.50727"/>
10 </startup>
11 </configuration>
2 <configuration>
3 <connectionStrings>
4 <add name="HairCuting.Properties.Settings.DataBaseConnectionString"
5 connectionString="Data Source=|DataDirectory|\DataBase.sdf"
6 providerName="Microsoft.SqlServerCe.Client.3.5" />
7 </connectionStrings>
8 <startup>
9 <supportedRuntime version="v2.0.50727"/>
10 </startup>
11 </configuration>
小研究了一下,发现了一个新类:Settings,可以方便读取其中的数据库连接字符串。
1 Settings settings = new Settings();
2 string connectionString = settings.DataBaseConnectionString.ToString();
2 string connectionString = settings.DataBaseConnectionString.ToString();