连接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]

本文介绍了如何在ASP.NET中连接Access数据库进行增、删、改、查操作。通过创建一个公共类ConnApp实现数据库连接,并展示了使用System.Web.HttpContext.Current.Server.MapPath获取数据库文件路径的方法。同时,文中提醒注意当字段名与Access关键字冲突时,需使用方括号包裹字段名。
最低0.47元/天 解锁文章
946

被折叠的 条评论
为什么被折叠?



