在SQL Server web 应用程序中,数据库的链接字符串基本上都写在了web.config 中,这里总结了一些数据库链接字符串的不同写法,但它们的功能都相同,下面主要列出了这些不同的写法,可供初学者参考使用
方法一:.在web.config中ConnectionString加入数据库连接字串
<connectionStrings>
<add name="constr" connectionString="Data Source=127.0.0.1;Initial Catalog=eoexma;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
获取的办法是:
ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
方法二:在aspx的html中
<appSetting>
<add key="constr" value="server=localhost;database=***;uid=sa;pwd=***"></add>
</appSetting>
获取办法是:
ConfigurationSettings.AppSettings["constr"];
方法三:
<connectionStrings>
<add name="constr" connectionString="server=127.0.0.1;DataBase=aa;uid=sa;pwd=a;min pool size=4;max pool size=4;packet size=3072""
providerName="System.Data.SqlClient" />
</connectionStrings>
获取办法是:
ConfigurationSettings.AppSettings["constr"];