public class DBCon
{
private OleDbConnection conn = null;
private string strCon;
public DBCon()
{
strCon = HttpContext.Current.Server.MapPath("~/Data/test.mdb");
}
//连接access数据库
public OleDbConnection getConn()
{
try
{
if (conn == null || conn.State == ConnectionState.Closed)
{
conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + strCon);
}
}
catch (Exception e)
{
throw e;
}
return conn;
}
}
public class DBCon
{
private SqlConnection conn = null;
private string strCon;
public DBCon()
{
strCon = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
}
//连接sqlserver2000 或者 2005数据库
public SqlConnection getConn()
{
try
{
if (conn == null || conn.State == ConnectionState.Closed)
{
conn = new SqlConnection(strCon);
}
}
catch (Exception e)
{
throw e;
}
return conn;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yangliuyilovexi/archive/2009/12/17/5027264.aspx