首页 > 开发 > 综合 > 正文

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

2024-07-21 02:45:02
字体:
来源:转载
供稿:网友
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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表