写下连接数据库的方法。供新同学学习下

王朝学院·作者佚名  2009-12-20  
宽屏版  字体: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有