C#连接数据库的新方法,通过web.config配置文件

分类: asp.net技术 2009-07-08 19:40 183人阅读 评论(0) 收藏举报

方法一、

1、web.config中<configuration>下加入以下连接代码

<connectionStrings>

<add name="Test" connectionString="server=.;u providerName="System.Data.SqlClient"/>

</connectionStrings>

2、在后台代码中加入以下代码,其中Test为web.config中数据连接的名称(name)

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString);

方法二、

1、web.config中<configuration>下加入以下连接代码

<appSettings>

<add key="Test" value="Data Source=.;Initial Catalog=tcedu;User />

</appSettings>

2、在后台代码中加入以下代码,其中Test为web.config中数据连接的名称(name)

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Test"]);