access数据库在应用了System.Data.OleDb后操作会很方便,这是一个常用的数据库操作类,其中两个方法,一个是返回datatable的,一个是执行sql语句返回影响记录的(一般是insert,update、delete等)。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.OleDb; /// <summary> ///DB 的摘要说明 /// </summary> public class DB { public string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("bubufx.mdb"); public DataTable GetDt(string sql) { DataSet ds = new DataSet(); OleDbConnection conn = new OleDbConnection(connStr); if (conn.State == ConnectionState.Closed) conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); da.Fill(ds); conn.Close(); return ds.Tables[0]; } public int RunSql(string sql) { try { OleDbConnection conn = new OleDbConnection(connStr); if (conn.State == ConnectionState.Closed) conn.Open(); OleDbCommand comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery(); conn.Close(); return 1; } catch { return 0; } } }