asp.net连接access数据库进行添加更新删除查询操作

来源:互联网 发布:软件培训学校骗局 编辑:程序博客网 时间:2024/04/29 23:43

连接access数据库代码,写在一个单独的类里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace AppWebDLL
{
    public class ConnApp
    {
        public OleDbConnection getCon() {
          
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Web.HttpContext.Current.Server.MapPath("App_Data//UserDB.mdb")+";Jet OLEDB:Database Password=admin";
            OleDbConnection objConnection = new OleDbConnection(strConnection);
            return objConnection;
        }
    }
}
我这习惯这样写的,在这里System.Web.HttpContext.Current.Server.MapPath是用来取得access的相对路径的,在这个类里想要使用System.Web.HttpContext.Current.Server.MapPath,先要引用system.web这个类。

 

 

增删改查代码,以我的类写的,这里要注意一个,如果你的字段和access的关键字一样,那么你就要在你的字段上加 [ ],如password是access的关键字,如果你的字段有这个,你在做操作的时候就要这样写[password]

 


原创粉丝点击