连接Access数据库
web.config添加配置
<connectionStrings> <add name="connStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Example.mdb" providerName="System.Data.OleDb"/> </connectionStrings>
App_Data文件夹中放
App_Start文件夹放帮助类:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; /// <summary> /// 数据库操作类 /// </summary> public class Common { private static OleDbConnection conn = new OleDbConnection(); private static OleDbCommand comm = new OleDbCommand(); public Common() { } /// <summary> /// 打开连接 /// </summary> private static void openConnection() { if (conn.State == ConnectionState.Closed) { try { conn.ConnectionString =System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString; comm.Connection = conn; conn.Open(); } catch (Exception e) { throw new Exception(e.Message); } } } /// <summary> /// 关闭连接 /// </summary> private static void closeConnection() { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); comm.Dispose(); } /// <summary> /// 执行一条sql语句 /// </summary> /// <param name="sqlStr">sql语句</param> public static void ExecuteSql(string sqlStr) { try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlStr; comm.ExecuteNonQuery(); } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } } /// <summary> /// 返回一个数据集 /// </summary> /// <param name="sqlStr">sql语句</param> /// <returns></returns> public static DataSet dataSet(string sqlStr) { OleDbDataAdapter da = new OleDbDataAdapter(); DataSet ds = new DataSet(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlStr; da.SelectCommand = comm; da.Fill(ds); } catch(Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return ds; } /// <summary> /// 返回一个数据视图 /// </summary> /// <param name="sqlStr">sql语句</param> /// <returns></returns> public static DataView dataView(string sqlStr) { OleDbDataAdapter da = new OleDbDataAdapter(); DataView dv = new DataView(); DataSet ds = new DataSet(); try { openConnection(); comm.CommandType = CommandType.Text; comm.CommandText = sqlStr; da.SelectCommand = comm; da.Fill(ds); dv = ds.Tables[0].DefaultView; } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return dv; } }
或用这个帮助类:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; /// <summary> /// DBHelper 的摘要说明 /// </summary> public class DBHelper { public static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; public DBHelper() { // // TODO: 在此处添加构造函数逻辑 // } public DataSet GetDataSet(string sql) { OleDbConnection conn = new OleDbConnection(connStr); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds); return ds; } public bool ExecSql(string sql) { bool IsSucceed = false; OleDbConnection conn = new OleDbConnection(connStr); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn); try { cmd.ExecuteNonQuery(); conn.Close(); IsSucceed = true; } catch (Exception e) { throw e; } return IsSucceed; } public OleDbDataReader GetReader(string sqlStr) { OleDbDataReader dr = null; OleDbConnection conn = new OleDbConnection(connStr); OleDbCommand cmd = new OleDbCommand(sqlStr, conn); conn.Open(); try { dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { conn.Close(); } return dr; } public int GetExexScalar(string sqlStr) { int ret = 0; OleDbConnection conn = new OleDbConnection(connStr); OleDbCommand cmd = new OleDbCommand(sqlStr, conn); conn.Open(); try { ret = (int)cmd.ExecuteScalar(); } finally { conn.Close(); } return ret; } public string GetExexScalarString(string sqlStr) { string ret = ""; OleDbConnection conn = new OleDbConnection(connStr); OleDbCommand cmd = new OleDbCommand(sqlStr, conn); conn.Open(); try { ret = (string)cmd.ExecuteScalar(); } finally { conn.Close(); } return ret; } }
test:
protected string GetTopNews() { StringBuilder sb = new StringBuilder(); string sqlStr = "select * from Employee"; DataTable dt = Common.dataSet(sqlStr).Tables[0];
// DataTable dt = new DBHelper().GetDataSet(sqlStr).Tables[0]; if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { sb.Append("<li>" + dt.Rows[i]["ID"] + ":" + dt.Rows[i]["EmpAddress"] + "</li>"); } } return sb.ToString(); }
此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。