在asp.net1.X里,所有人都会这样在web.config里写连接字符串,如:
<
appSettings
>
<
add key
=
"
ConnectionString
"
value
=
"
server=*.*.*.*;database=??;User ID=sa;password=sa
"
/>
</
appSettings
>
然后在.cs文件里这样引用
ConfigurationSettings.AppSettings[
"
ConnectionString
"
].ToString ();
然后大部分人也都会这样在2.0里写,问题出来了,嘿嘿.恭喜,又要进步了.在网上搜索了一下,结果很多人问这个问题,有些人(不知道有没用过2.0)就直接用1.x的方法去回答人家,这是误导啊
解决方法如下:
在web.config里应该这样写
<
configuration
>
<
appSettings
/>
<
connectionStrings
>
<
add connectionString
=
"
server=localhost;database=db;uid=sa;pwd=xxx
"
name
=
"
ConnStr
"
providerName
=
"
System.Data.SqlClient
"
/>
</
connectionStrings
>
在.cs里应该这样引用:
System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ToString()