前言
最近项目用到了access,是的就是access,工作在桌面型的小数据库应用还是会用到的,如果你确定永远不会遇到access的操作,请忽略此篇文章
1.vs配置access
既然是数据库,就少不了增删改查。不像mssql那样access没有存储过程(但可以进行宏操作),我们一般都是写T-SQL操作。
1.1.附上access的db操作辅助类。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.OleDb; namespace DB { class OdbcDBHelper : DBHelper { ////// 读取二进制字段 /// /// 存储过程或者sql语句 /// 参数列表 /// /// 超时时间 /// public override Byte[] GetBinary(string sql, IList pars, CommandType cmdType) { return GetBinary(sql, pars, cmdType, comTimeout); } /// /// 读取二进制字段 /// /// 存储过程或者sql语句 /// 参数列表 /// /// 超时时间 /// public override Byte[] GetBinary(string sql, IList pars, CommandType cmdType, int comTimeout_) { using (OleDbConnection SConn = new OleDbConnection(connText)) { SConn.Open(); DataSet ds = new DataSet(); try { OleDbCommand cmd = new OleDbCommand(); if (pars != null) { OleDbParameter sqlp = null; foreach (JHSqlParameter p in pars) { sqlp = new OleDbParameter(); sqlp.ParameterName = p.ParameterName; sqlp.Value = p.Value; sqlp.Direction = p.Direction; cmd.Parameters.Add(sqlp); } } cmd.CommandTimeout = comTimeout; cmd.CommandText = sql; cmd.CommandType = cmdType; cmd.Connection = SConn; OleDbDataReader sdr = cmd.ExecuteReader(); sdr.Read(); Byte[] blob = null; blob = new Byte[(sdr.GetBytes(0, 0, null, 0, int.MaxValue))]; sdr.GetBytes(0, 0, blob, 0, blob.Length); sdr.Close(); cmd.Parameters.Clear(); return blob; } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { SConn.Close(); } } } ///// ///// 前端获得cmd对象 ///// ///// 存储过程的名字 ///// SqlCommand对象 //public override OleDbCommand GetSqlCommand(string sql) //{ // return GetSqlCommand(sql, comTimeout); //} ///// ///// 前端获得cmd对象 ///// ///// 存储过程的名字 ///// 超时时间 ///// SqlCommand对象 //public override OleDbCommand GetSqlCommand(string sql, int comTimeout_) //{ // OleDbConnection SConn = new OleDbConnection(connText); // OleDbCommand cmd = new OleDbCommand(); // cmd.CommandTimeout = comTimeout_; // cmd.CommandText = sql; // cmd.CommandType = CommandType.StoredProcedure; // cmd.Connection = SConn; // return cmd; //} /// /// 执行存储过程或者sql语句 /// /// /// /// /// 受影响的行数 public override int ExecuteNonQuery(string sql, IList pars, CommandType cmdType) { return ExecuteNonQuery(sql, pars, cmdType, comTimeout); } /// /// 执行存储过程或者sql语句 /// /// /// /// /// 受影响的行数 public override int ExecuteNonQuery(string sql, IList pars, CommandType cmdType, int comTimeout_) { using (OleDbConnection SConn = new OleDbConnection(connText)) { SConn.Open(); int i = 0; try { OleDbCommand cmd = new OleDbCommand(); if (pars != null) { OleDbParameter sqlp = null; foreach (JHSqlParameter p in pars) { sqlp = new OleDbParameter(); sqlp.ParameterName = p.ParameterName; sqlp.Value = p.Value; sqlp.Direction = p.Direction; if (p.Size > 0) { sqlp.Size = p.Size; } cmd.Parameters.Add(sqlp); } } cmd.CommandTimeout = comTimeout_; cmd.CommandText = sql; cmd.CommandType = cmdType; cmd.Connection = SConn; i = cmd.ExecuteNonQuery(); for (int j = 0; j < cmd.Parameters.Count; j++) { if (cmd.Parameters[j].Direction == ParameterDirection.InputOutput || cmd.Parameters[j].Direction == ParameterDirection.Output) { pars[j].Value = cmd.Parameters[j].Value; } } cmd.Parameters.Clear(); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { SConn.Close(); } return i; } } /// /// 执行存储过程或者sql语句_事务 /// /// /// /// /// void public override void ExecuteNonQuery_Tran(string[] sql, IList [] pars, CommandType[] cmdType) { ExecuteNonQuery_Tran(sql, pars, cmdType, comTimeout); } /// /// 执行存储过程或者sql语句_事务 /// /// /// /// /// void public override void ExecuteNonQuery_Tran(string[] sql, IList [] pars, CommandType[] cmdType, int comTimeout_) { using (OleDbConnection SConn = new OleDbConnection(connText)) { SConn.Open(); OleDbTransaction myTran = SConn.BeginTransaction(); try { OleDbCommand cmd = new OleDbCommand(); cmd.Transaction = myTran; cmd.Connection = SConn; for (int tt = 0; tt < sql.Length; tt++) { if (pars[tt] != null) { OleDbParameter sqlp = null; foreach (JHSqlParameter p in pars[tt]) { sqlp = new OleDbParameter(); sqlp.ParameterName = p.ParameterName; sqlp.Value = p.Value; sqlp.Direction = p.Direction; if (p.Size > 0) { sqlp.Size = p.Size; } cmd.Parameters.Add(sqlp); } } cmd.CommandTimeout = comTimeout_; cmd.CommandText = sql[tt]; cmd.CommandType = cmdType[tt]; cmd.ExecuteNonQuery(); for (int j = 0; j < cmd.Parameters.Count; j++) { if (cmd.Parameters[j].Direction == ParameterDirection.InputOutput || cmd.Parameters[j].Direction == ParameterDirection.Output) { pars[tt][j].Value = cmd.Parameters[j].Value; } } cmd.Parameters.Clear(); } myTran.Commit(); } catch (Exception ex) { myTran.Rollback(); throw new ApplicationException(ex.Message); } finally { SConn.Close(); } } } /// /// 返回第一行第一列的值 /// /// /// /// /// public override string ExecuteScalar(string sql, IList pars, CommandType cmdType) { return ExecuteScalar(sql, pars, cmdType, comTimeout); } /// /// 返回第一行第一列的值 /// /// 存储过程/语句 /// 参数列表 /// /// public override string ExecuteScalar(string sql, IList pars, CommandType cmdType, int comTimeout_) { using (OleDbConnection SConn = new OleDbConnection(connText)) { SConn.Open(); string str = null; try { OleDbCommand cmd = new OleDbCommand(); if (pars != null) { OleDbParameter sqlp = null; foreach (JHSqlParameter p in pars) { sqlp = new OleDbParameter(); sqlp.ParameterName = p.ParameterName; sqlp.Value = p.Value; sqlp.Direction = p.Direction; cmd.Parameters.Add(sqlp); } } cmd.CommandTimeout = comTimeout_; cmd.CommandText = sql; cmd.CommandType = cmdType; cmd.Connection = SConn; object obj = cmd.ExecuteScalar(); str = obj == null ? "" : obj.ToString(); cmd.Parameters.Clear(); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { SConn.Close(); } return str; } } /// /// 获得DataTable /// /// 存储过程/语句 /// 参数列表 /// public override DataTable GetDataTable(string sql, IList pars, CommandType cmdType) { return GetDataTable(sql, pars, cmdType, comTimeout); } /// /// 获得DataTable /// /// /// /// public override DataTable GetDataTable(string sql, IList pars, CommandType cmdType, int comTimeout_) { using (OleDbConnection SConn = new OleDbConnection(connText)) { SConn.Open(); DataSet ds = new DataSet(); try { OleDbCommand cmd = new OleDbCommand(); if (pars != null) { OleDbParameter sqlp = null; foreach (JHSqlParameter p in pars) { sqlp = new OleDbParameter(); sqlp.ParameterName = p.ParameterName; sqlp.Value = p.Value; sqlp.Direction = p.Direction; cmd.Parameters.Add(sqlp); } } cmd.CommandTimeout = comTimeout_; cmd.CommandText = sql; cmd.CommandType = cmdType; cmd.Connection = SConn; OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(ds); cmd.Parameters.Clear(); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { SConn.Close(); } return ds.Tables[0]; } } } }
1.2.以下是在我们项目中config配置
注:其实就是在db操作的时候执行上面db操作辅助类就行了。
这个类库是我们封装的db操作的类库
access是oledb驱动进行操作的
找到这个对应的配置
这样就行了,然后写t-sql就行了,其实和mssq基本没区别
1.3.config配置地址(access2007版本):
注:上面的配置进行字符串加密,以下是具体链接配置。
其中Microsoft.ACE.OLEDB.12.0;是access2007及以上。Microsoft.Jet.OLEDB.4.0;是access2003。操作过access导出的应该知道这一点,不再累述。
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\192.168.1.207\db\mydata.accdb;Jet OLEDB:database password=123456;Persist Security Info=False
2.远程连接access
用远程链接的目的就是在团队开发的时候共用一个access文件,方便协作。额外要求不可开发人员访问服务器,所以就想到了文件共享。
2.1.局域网文件共享(还有其他方式,在这只记录我自己的操作用的)
网络----属性----更改高级共享设置
公用启动网络发现
右击文件共享
选择everyone添加
关闭防火墙。
选择更改设置