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

 

分类: asp.net技术 

方法一、   
1、web.config中<configuration>下加入以下连接代码   
    <connectionStrings>          
        <add name="Test" connectionString="server=.;uid=sa;pwd=;Database=tcedu;Connect Timeout=90" 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 ID=sa;password="/>   
  
    </appSettings>   
2、在后台代码中加入以下代码,其中Test为web.config中数据连接的名称(name)   
  
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Test"]);

posted @ 2016-11-05 00:40  陈_连海  阅读(2124)  评论(1编辑  收藏  举报