Connection with Web.config

先將Web.config加入

<appSettings>
<add key="strConnection_sqlserver" value="data source=HUIQIN;initial catalog=SDTE;user id=administrator;password=xiaohan;persist security info=False;packet size=4096" />
<add key="strConnection_oracle" value="user id=system;data source=kflwn;password=manager" />
</appSettings>

然後在cs中加入

using System.Configuration;
public class FileConn
 
{
  
private static string strconn_sqlserver=System.Configuration.ConfigurationSettings.AppSettings["strConnection_sqlserver"];
  
private static string strconn_oracle=System.Configuration.ConfigurationSettings.AppSettings["strConnection_oracle"];
  
public  string adminname="";
  
public  string adminpass="";
  
  
public static SqlConnection getConn_sqlserver(){
   SqlConnection conn
=new SqlConnection(FileConn.strconn_sqlserver);
   
return conn;
  }

  
public static OracleConnection getConn_oracle()
  
{
   OracleConnection conn
=new OracleConnection(FileConn.strconn_oracle);
   
return conn;
  }

  
public static DataSet getMessage_oracle(string strSQL){
   OracleConnection conn
=FileConn.getConn_oracle();
   
try{
    conn.Open();
    OracleDataAdapter sda
=new OracleDataAdapter(strSQL,conn);
    DataSet ds
=new DataSet("ds");
    sda.Fill(ds);
    
return ds;
   }

   
catch(OracleException e){
    
throw new Exception(e.Message);
   }

  }

  
public static DataSet getMessage_sqlserver(string strSQL)
  
{
   SqlConnection conn
=FileConn.getConn_sqlserver();
   
try
   
{
    conn.Open();
    SqlDataAdapter sda
=new SqlDataAdapter(strSQL,conn);
    DataSet ds
=new DataSet("ds");
    sda.Fill(ds);
    
return ds;
   }

   
catch(System.Data.SqlClient.SqlException e)
   
{
    
throw new Exception(e.Message);
   }

  }

  
public static DataTable GetUsers(){
   DataSet ds;
   
if(HttpContext.Current.Cache["usersDS"]==null){
    ds
=new DataSet();
    ds.ReadXml(HttpContext.Current.Server.MapPath(
"User.config"),XmlReadMode.Auto);
    ds.Tables[
0].Columns["Login"].Unique=true;
    HttpContext.Current.Cache.Insert(
"usersDS",ds,new CacheDependency(HttpContext.Current.Server.MapPath("User.config")));
   }

   
return((DataSet)HttpContext.Current.Cache["usersDS"]).Tables[0];
  }

posted on 2006-04-11 21:53  freeliver54  阅读(271)  评论(0编辑  收藏  举报

导航