在App.Config文件加下两个节点中任意一个<appSettings>……</appSettings>或
<
connectionStrings
>……
</
connectionStrings
>
App.config
<? xml version ="1.0" encoding ="utf-8" ?>
< configuration >
< appSettings >
< add key ="constri" value ="Data Source=.;Initial Catalog=travel;Integrated Security=True" />
</ appSettings >
< connectionStrings >
< add name ="constri" connectionString ="Data Source=.;Initial Catalog=travel;Integrated Security=True" />
</ connectionStrings >
</ configuration >
App.config
<? xml version ="1.0" encoding ="utf-8" ?>
< configuration >
< appSettings >
< add key ="constri" value ="Data Source=.;Initial Catalog=travel;Integrated Security=True" />
</ appSettings >
< connectionStrings >
< add name ="constri" connectionString ="Data Source=.;Initial Catalog=travel;Integrated Security=True" />
</ connectionStrings >
</ configuration >
然后在.cs文件中的读取连接字符串的方法为:
using System.Configuration;
1 string constr = ConfigurationSettings.AppSettings[ "constri"].ToString ();
2 //string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constri"].ConnectionString;
1 string constr = ConfigurationSettings.AppSettings[ "constri"].ToString ();
2 //string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constri"].ConnectionString;
首先引入命名空间,第1行是如果config文件加入的是<appSettings>节点的读取方式,第二行是另一种字节的读取方法。
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185799如需转载请自行联系原作者
叶子文文