using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Data; using System.Data.OleDb; //添加引用 using System.Web; namespace xxxDAL { public class DBHelper { //引导<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>连接数据库调用Web.Config文件 private static OleDbConnection connection; private static String constr = System.Configuration.ConfigurationManager.AppSettings["provider"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]) + ";"; //创建连接|DataDirectory|/SchoolDB.mdb public static OleDbConnection Connection { get { OleDbConnection myConn = new OleDbConnection(constr); string connectionString = myConn.ConnectionString; if (connection == null) { connection = new OleDbConnection(connectionString); //打开连接 connection.Open(); } else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } } //(无参)返回执行的行数(删除修改更新) public static int ExuteCommand(string safeSql) { OleDbCommand cmd = new OleDbCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } //(有参) public static int ExuteCommand(string sql, params OleDbParameter[] values) { OleDbCommand cmd = new OleDbCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } //(无参)返回第一行第一列(删除修改更新) public static int GetScalar(string safeSql) { OleDbCommand cmd = new OleDbCommand(safeSql, Connection); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } //(有参) public static int GetScalar(string sql, params OleDbParameter[] values) { OleDbCommand cmd = new OleDbCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } //返回一个DataReader(查询) public static OleDbDataReader GetReader(string safeSql) { OleDbCommand cmd = new OleDbCommand(safeSql, Connection); OleDbDataReader reader = cmd.ExecuteReader(); return reader; } public static OleDbDataReader GetReader(string sql, params OleDbParameter[] values) { OleDbCommand cmd = new OleDbCommand(sql, Connection); cmd.Parameters.AddRange(values); OleDbDataReader reader = cmd.ExecuteReader(); return reader; } //返回一个DataTable public static DataTable GetTable(string safeSql) { DataSet ds = new DataSet(); OleDbCommand cmd = new OleDbCommand(safeSql, Connection); OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } public static DataTable GetTable(string sql, params OleDbParameter[] values) { DataSet ds = new DataSet(); OleDbCommand cmd = new OleDbCommand(sql, Connection); cmd.Parameters.AddRange(values); OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } } } web.config中需要添加 <add key="provider" value="Provider=Microsoft.Jet.OLEDB.4.0;data source="/> <add key="dbPath" value="~/App_Data/xxx(你的数据库).mdb"/>