1
<configuration>
2
<appSettings>
3
<add key="sqlconn" value= server=localhost;database=data;uid=sa;pwd=sa"/>
4
</appSettings>
5
<connectionStrings>
6
<add name="DataConnectionString" connectionString="Data Source=localhost;Initial Catalog=Data;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
7
</connectionStrings>
8
</configuration>

2

3

4

5

6

7

8

注意:
(1)AppSettings 是在2003中常用的,ConnectionStrins是2005中常用的.
(2)使用ConnectionString的好处:
第一:可将连接字符串加密,使用MS的一个加密工具即可。
第二:可直接邦定的数据源控件,而不必写代码读出来再赋值给控件。
第三:可方便的更换数据库平台,如换为Oracle数据库,只需修改providerName。
(3)写在 <appSettings >中用System.Configuration.ConfigurationManager.AppSettings["name"]检索值。
写在 <ConnectionStrings>中用System.Configuration.ConfigurationManager.ConnectionStrings["name"]检索值。