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

来源:互联网 发布:linux查看php日志文件 编辑:程序博客网 时间:2024/05/22 07:56
方法一、   
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"]);  
0 0