C#操作Access的一些小结

C#操作Access的一些小结

好久没有写blog,感觉今年一年都没怎么真正开心过,整天有一些事围绕在身边,使心情难以平静下来,真正写点有意义的东西。博客园是天天看的,看得多,写的少,偶尔也是Copy一篇技术文章放一下,便于自己将来查询。

最近有Winfom做了一个小系统,后台数据库是用Access,主要是单机版,考虑方便性,现在总结一些值得后来人参考的地方。

一,数据库操作基类

一般都要写个基类,这样,将来换数据库,换一下这个DA基类就行了。这个类也是从网上找的。不过感觉有的挺不错,一般就要返回dataset,datatable,datarow。还有执行sql语句,这里主要好的地方是可以带params执行,另一个就是支持事务。拿出来和大家共享一下。

?
?

代码

?public?static?class?AccessHelper
????{
????????//数据库连接字符串
????????//WebForm
????????//public?static?readonly?string?conn_str?=?"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?HttpContext.Current.Request.PhysicalApplicationPath?+?System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

????????//WinForm
????????//public?static?readonly?string?conn_str?=?"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?AppDomain.CurrentDomain.BaseDirectory?+?System.Configuration.ConfigurationManager.ConnectionStrings["WZDFGL"].ConnectionString;
????????//public?static?readonly?string?conn_str?=??System.Configuration.ConfigurationManager.ConnectionStrings["WZDFGL"].ConnectionString;

????????public?static?readonly?string?conn_str?=?"Provider=Microsoft.Jet.OLEDB.4.0;Data?Source="?+?AppDomain.CurrentDomain.BaseDirectory?+?System.Configuration.ConfigurationManager.ConnectionStrings["WZDFGL"].ConnectionString?+?";Persist?Security?Info=True";

????????//?用于缓存参数的HASH表
????????private?static?Hashtable?parmCache?=?Hashtable.Synchronized(new?Hashtable());
????????///?<summary>
????????///??给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
????????///?</summary>
????????///?<param?name="connectionString">一个有效的连接字符串</param>
????????///?<param?name="commandText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>执行命令所影响的行数</returns>
????????public?static?int?ExecuteNonQuery(string?connectionString,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????using?(OleDbConnection?conn?=?new?OleDbConnection(connectionString))
????????????{
????????????????PrepareCommand(cmd,?conn,?null,?cmdText,?commandParameters);
????????????????int?val?=?cmd.ExecuteNonQuery();
????????????????cmd.Parameters.Clear();
????????????????return?val;
????????????}
????????}
????????///?<summary>
????????///?用现有的数据库连接执行一个sql命令(不返回数据集)
????????///?</summary>
????????///?<remarks>
????????///举例:??
????????///??int?result?=?ExecuteNonQuery(connString,?"PublishOrders",?new?OleDbParameter("@prodid",?24));
????????///?</remarks>
????????///?<param?name="conn">一个现有的数据库连接</param>
????????///?<param?name="commandText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>执行命令所影响的行数</returns>
????????public?static?int?ExecuteNonQuery(OleDbConnection?connection,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????PrepareCommand(cmd,?connection,?null,?cmdText,?commandParameters);
????????????int?val?=?cmd.ExecuteNonQuery();
????????????cmd.Parameters.Clear();
????????????return?val;
????????}
????????///?<summary>
????????///使用现有的SQL事务执行一个sql命令(不返回数据集)
????????///?</summary>
????????///?<remarks>
????????///举例:??
????????///??int?result?=?ExecuteNonQuery(trans,?"PublishOrders",?new?OleDbParameter("@prodid",?24));
????????///?</remarks>
????????///?<param?name="trans">一个现有的事务</param>
????????///?<param?name="commandText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>执行命令所影响的行数</returns>
????????public?static?int?ExecuteNonQuery(OleDbTransaction?trans,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????PrepareCommand(cmd,?trans.Connection,?trans,?cmdText,?commandParameters);
????????????int?val?=?cmd.ExecuteNonQuery();
????????????cmd.Parameters.Clear();
????????????return?val;
????????}
????????///?<summary>
????????///?用执行的数据库连接执行一个返回数据集的sql命令
????????///?</summary>
????????///?<remarks>
????????///?举例:??
????????///??OleDbDataReader?r?=?ExecuteReader(connString,?"PublishOrders",?new?OleDbParameter("@prodid",?24));
????????///?</remarks>
????????///?<param?name="connectionString">一个有效的连接字符串</param>
????????///?<param?name="commandText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>包含结果的读取器</returns>
????????public?static?OleDbDataReader?ExecuteReader(string?connectionString,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????//创建一个SqlCommand对象
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????//创建一个SqlConnection对象
????????????OleDbConnection?conn?=?new?OleDbConnection(connectionString);
????????????//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
????????????//因此commandBehaviour.CloseConnection?就不会执行
????????????try
????????????{
????????????????//调用?PrepareCommand?方法,对?SqlCommand?对象设置参数
????????????????PrepareCommand(cmd,?conn,?null,?cmdText,?commandParameters);
????????????????//调用?SqlCommand??的?ExecuteReader?方法
????????????????OleDbDataReader?reader?=?cmd.ExecuteReader(CommandBehavior.CloseConnection);
????????????????//清除参数
????????????????cmd.Parameters.Clear();
????????????????return?reader;
????????????}
????????????catch
????????????{
????????????????//关闭连接,抛出异常
????????????????conn.Close();
????????????????throw;
????????????}
????????}
????????///?<summary>
????????///?返回一个DataSet数据集
????????///?</summary>
????????///?<param?name="connectionString">一个有效的连接字符串</param>
????????///?<param?name="cmdText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>包含结果的数据集</returns>
????????public?static?DataSet?ExecuteDataSet(string?connectionString,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????//创建一个SqlCommand对象,并对其进行初始化
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????using?(OleDbConnection?conn?=?new?OleDbConnection(connectionString))
????????????{
????????????????PrepareCommand(cmd,?conn,?null,?cmdText,?commandParameters);
????????????????//创建SqlDataAdapter对象以及DataSet
????????????????OleDbDataAdapter?da?=?new?OleDbDataAdapter(cmd);
????????????????DataSet?ds?=?new?DataSet();
????????????????try
????????????????{
????????????????????//填充ds
????????????????????da.Fill(ds);
????????????????????//?清除cmd的参数集合?
????????????????????cmd.Parameters.Clear();
????????????????????//返回ds
????????????????????return?ds;
????????????????}
????????????????catch
????????????????{
????????????????????//关闭连接,抛出异常
????????????????????conn.Close();
????????????????????throw;
????????????????}
????????????}
????????}

????????public?static?DataSet?ExecuteDataSet(OleDbConnection?conn,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????//创建一个SqlCommand对象,并对其进行初始化
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????PrepareCommand(cmd,?conn,?null,?cmdText,?commandParameters);
????????????//创建SqlDataAdapter对象以及DataSet
????????????OleDbDataAdapter?da?=?new?OleDbDataAdapter(cmd);
????????????DataSet?ds?=?new?DataSet();
????????????try
????????????{
????????????????//填充ds
????????????????da.Fill(ds);
????????????????//?清除cmd的参数集合?
????????????????cmd.Parameters.Clear();
????????????????//返回ds
????????????????return?ds;
????????????}
????????????catch
????????????{
????????????????//关闭连接,抛出异常
????????????????conn.Close();
????????????????throw;
????????????}
????????}

????????///?<summary>
????????///?用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列
????????///?</summary>
????????///?<remarks>
????????///例如:??
????????///??Object?obj?=?ExecuteScalar(connString,?"PublishOrders",?new?OleDbParameter("@prodid",?24));
????????///?</remarks>
????????///<param?name="connectionString">一个有效的连接字符串</param>
????????///?<param?name="commandText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>用?Convert.To{Type}把类型转换为想要的?</returns>
????????public?static?object?ExecuteScalar(string?connectionString,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????using?(OleDbConnection?connection?=?new?OleDbConnection(connectionString))
????????????{
????????????????PrepareCommand(cmd,?connection,?null,?cmdText,?commandParameters);
????????????????object?val?=?cmd.ExecuteScalar();
????????????????cmd.Parameters.Clear();
????????????????return?val;
????????????}
????????}
????????///?<summary>
????????///?用指定的数据库连接执行一个命令并返回一个数据集的第一列
????????///?</summary>
????????///?<remarks>
????????///?例如:??
????????///??Object?obj?=?ExecuteScalar(connString,?"PublishOrders",?new?OleDbParameter("@prodid",?24));
????????///?</remarks>
????????///?<param?name="conn">一个存在的数据库连接</param>
????????///?<param?name="commandText">存储过程名称或者sql命令语句</param>
????????///?<param?name="commandParameters">执行命令所用参数的集合</param>
????????///?<returns>用?Convert.To{Type}把类型转换为想要的?</returns>
????????public?static?object?ExecuteScalar(OleDbConnection?connection,?string?cmdText,?params?OleDbParameter[]?commandParameters)
????????{
????????????OleDbCommand?cmd?=?new?OleDbCommand();
????????????PrepareCommand(cmd,?connection,?null,?cmdText,?commandParameters);
????????????object?val?=?cmd.ExecuteScalar();
????????????cmd.Parameters.Clear();
????????????return?val;
????????}
????????///?<summary>
????????///?将参数集合添加到缓存
????????///?</summary>
????????///?<param?name="cacheKey">添加到缓存的变量</param>
????????///?<param?name="cmdParms">一个将要添加到缓存的sql参数集合</param>
????????public?static?void?CacheParameters(string?cacheKey,?params?OleDbParameter[]?commandParameters)
????????{
????????????parmCache[cacheKey]?=?commandParameters;
????????}
????????///?<summary>
????????///?找回缓存参数集合
????????///?</summary>
????????///?<param?name="cacheKey">用于找回参数的关键字</param>
????????///?<returns>缓存的参数集合</returns>
????????public?static?OleDbParameter[]?GetCachedParameters(string?cacheKey)
????????{
????????????OleDbParameter[]?cachedParms?=?(OleDbParameter[])parmCache[cacheKey];
????????????if?(cachedParms?==?null)
????????????????return?null;
????????????OleDbParameter[]?clonedParms?=?new?OleDbParameter[cachedParms.Length];
????????????for?(int?i?=?0,?j?=?cachedParms.Length;?i?<?j;?i++)
????????????????clonedParms?=?(OleDbParameter[])((ICloneable)cachedParms).Clone();
????????????return?clonedParms;
????????}
????????///?<summary>
????????///?准备执行一个命令
????????///?</summary>
????????///?<param?name="cmd">sql命令</param>
????????///?<param?name="conn">Sql连接</param>
????????///?<param?name="trans">Sql事务</param>
????????///?<param?name="cmdText">命令文本,例如:Select?*?from?Products</param>
????????///?<param?name="cmdParms">执行命令的参数</param>
????????private?static?void?PrepareCommand(OleDbCommand?cmd,?OleDbConnection?conn,?OleDbTransaction?trans,?string?cmdText,?OleDbParameter[]?cmdParms)
????????{
????????????//判断连接的状态。如果是关闭状态,则打开
????????????if?(conn.State?!=?ConnectionState.Open)
????????????????conn.Open();
????????????//cmd属性赋值
????????????cmd.Connection?=?conn;
????????????cmd.CommandText?=?cmdText;
????????????//是否需要用到事务处理
????????????if?(trans?!=?null)
????????????????cmd.Transaction?=?trans;
????????????cmd.CommandType?=?CommandType.Text;
????????????//添加cmd需要的存储过程参数
????????????if?(cmdParms?!=?null)
????????????{
????????????????foreach?(OleDbParameter?parm?in?cmdParms)
????????????????????cmd.Parameters.Add(parm);
????????????}
????????}

????}

?
?

?
?

?
?

如新增:

?
?

代码

public?int?InsertCby(CbyEntity?objCbyEntity)
????????{
????????????int?intResult?=?0;
????????????string?strSql?=?"insert?into?cby?(dh,xm)??values?(@dh,@xm)";
????????????OleDbParameter[]?prams?=?{
?????????????????????????????????????????????new?OleDbParameter("@dh",?objCbyEntity.Dh),
?????????????????????????????????????????????new?OleDbParameter("@xm",?objCbyEntity.Xm)
?????????????????????????????????????????};
????????????intResult?=?AccessHelper.ExecuteNonQuery(AccessHelper.conn_str,?strSql,?prams);
????????????return?intResult;
????????}

?
?

用事务:

?
?

代码

public?bool?InsertDfdjzkOrder(DfdjzkEntity?etyDfdj,?DfdjhmEntity?etyDfdjhm,?out?string?errmsg)
????????{
????????????bool?result?=?true;
????????????errmsg?=?"";
????????????string?strCon?=?AccessHelper.conn_str;
????????????OleDbConnection?con?=?new?OleDbConnection(strCon);
????????????try
????????????{
????????????????con.Open();
????????????????OleDbTransaction?tra?=?con.BeginTransaction();?//创建事务,开始执行事务
????????????????if?(this.InsertDfdjzk(tra,?etyDfdj)?!=?1)
????????????????{
????????????????????tra.Rollback();
????????????????????return?false;
????????????????}
????????????????DfdjhmDA?daDfdjhm?=?new?DfdjhmDA();
????????????????if?(daDfdjhm.UpdateDfdjhm(tra,?etyDfdjhm)?!=?1)
????????????????{
????????????????????tra.Rollback();
????????????????????return?false;
????????????????}

????????????????tra.Commit();//关闭事务
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????errmsg?=?ex.Message;
????????????????return?false;
????????????}
????????????finally
????????????{
????????????????con.Close();
????????????}

????????????return?result;

????????}

?
?

?
?

批量更新时,一定要用同一连接,不然效率肯定狂慢。

?
?

代码

?public?bool?UpdateXhRqBatch()
????????{
????????????using?(OleDbConnection?conn?=?new?OleDbConnection(AccessHelper.conn_str))
????????????{
????????????????string?strSql?=?"SELECT?*?FROM?dfdjzk?Where?1=1?";//and?djhm>‘00050235‘
????????????????DataSet?ds;
????????????????ds?=?AccessHelper.ExecuteDataSet(conn,?strSql,?null);

????????????????foreach?(DataRow?dr?in?ds.Tables[0].Rows)
????????????????{
????????????????????string?djhm?=?dr[DfdjzkSchema.DJHM].ToString();
????????????????????string?xhrq?=?dr[DfdjzkSchema.XHRQ].ToString();
????????????????????if?(!string.IsNullOrEmpty(xhrq)?&&?xhrq.Length?==?4)
????????????????????{
????????????????????????if?(xhrq.Substring(0,?2)?==?"99")
????????????????????????{
????????????????????????????xhrq?=?"19"?+?xhrq?+?"01";
????????????????????????}
????????????????????????else
????????????????????????{
????????????????????????????xhrq?=?"20"?+?xhrq?+?"01";
????????????????????????}

????????????????????????string?updatesql?=?"update?Dfdjzk?set?xhrq=‘"?+?xhrq?+?"‘?where?djhm?=‘"?+?djhm?+?"‘?";

????????????????????????AccessHelper.ExecuteNonQuery(conn,?updatesql,?null);

????????????????????}
????????????????}
????????????}
????????????return?true;

????????}

?
?

?
?

2,一些注意点

Access对日期操作时,sql语句要用#

?
?

代码

string?strSql?=?@"insert?into?Dfdjzk?(zhh,yhdw,bqss1,sqss1,syl1,dj1,je1,
????????????????????????????????????????????????????bqss2,sqss2,syl2,dj2,je2,
????????????????????????????????????????????????????bqss3,sqss3,syl3,dj3,je3,
????????????????????????????????????????????????????je,rzbz,zfbz,djhm,kprq,sssq,ssqh,xhrq,cby,sfy)
??????????????????????????????????????????values?(@zhh,@yhdw,@bqss1,@sqss1,@syl1,@dj1,@je1,
[email protected],@sqss2,@syl2,@dj2,@je2,
[email protected],@sqss3,@syl3,@dj3,@je3,
[email protected],@rzbz,@zfbz,@djhm,#"?+?objDfdjzkEntity.Kprq.ToString("yyyy-MM-dd")?+?"#,@sssq,@ssqh,@xhrq,@cby,@sfy)";

?
?

?
?

Access支持子查询,不过效率狂低,能不用最好不用,与SQL server没法比

//??????????? string strSql = @"SELECT zhh,ssqh,yhdw from yhdw where zhh<>‘‘ and (glbz=true or
//??????????????? exists (select 0 from dfdjzk where yhdw.zhh = zhh and sssq=‘" + sssq + @"‘)
//??????????????? )";

?
?

用ADO.NET模糊查询时,和SQL Server一样。和直接在Access查询设计器中是不一样的概念。

?
?

if (kplq_end != "")
??????????? {
??????????????? strSql += " And kprq<=#" + kplq_end + "# ";
??????????? }

??????????? if (kplq_bgn != "")
??????????? {
??????????????? strSql += " And kprq>=#" + kplq_bgn + "# ";
??????????? }

??????????? if (djhm != "")
??????????? {
??????????????? strSql += " And djhm like ‘%" + djhm + "%‘ ";
??????????? }

??????????? if (zhh != "")
??????????? {
??????????????? strSql += " And zhh like ‘%" + zhh + "%‘ ";
??????????? }

?
?

就先写这么多吧。

时间: 2024-11-08 13:09:20

C#操作Access的一些小结的相关文章

C# 操作access db

1:如何读取DataSet 例:对象为ds ds.Table[0].Rows[0]["字段"].Value 就可以取得2:如何写到access数据库 代码有点多,建议重新搜索一下[C# 操作access] 有很多代码可用 参考:http://zhidao.baidu.com/question/296512460.html3:一开始access 不存在那么存在如下几个问题 A:如何利用C#创建一个access文件 参考:http://zhidao.baidu.com/question/2

C#操作Access

本文以ADOX操作Access 1.在C#中新建AccessClass.cs文件,内容如下 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6 using System.IO; //包含File 7 using System.Data; //包含datatable 8 using System.Data

C#操作access练习

ORM框架使用dapper,dapper不仅能操作sqlserver,也能操作access,下面为基本代码: OleDbConnection strConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "db.mdb" + ";Persist Security Info=False"); //建立数据库引擎连接,注意数据表(后缀为.db

C#操作access和SQL server数据库代码实例

在C#的学习中,操作数据库是比较常用的技术,而access和sql server 数据库的操作却有着不同.那么,有哪些不同呢? 首先,需要引用不同的类.因为有着不同的数据引擎. access:using System.Data.OleDb; sql server:using System.Data.SqlClient; 下面是我写的access和sql 数据库操作的两个类: 1 //1.操作sql数据库的类 2 public class DBOperSQL1 3 { 4 public stati

Winform操作Access数据库增删改操作学习笔记

此程序是本人学习Winform操作Access数据库时学习笔记. 程序源码来源于:<Visual C# 2008 程序开发入门与提高 >赵增敏 编著 操作方法: 1.新建一个Winform项目,并将该项目设置为启动项目: 2.复制数据库文件到程序根目录下. 3.添加3个Label.两个TextBox.一个ListBox,四个Button控件. 4.窗体底部Label修改名称为“labelMsg”,两个文本框修改名称为:textboxSurname,textboxName. 5.列表框命名为Li

MFC通过ADO操作Access数据库

我在<VC知识库在线杂志>第十四期和第十五期上曾发表了两篇文章--"直接通过ODBC读.写Excel表格文件"和"直接通过DAO读.写Access文件",先后给大家介绍了ODBC和DAO两种数据库访问技术的基本使用方法,这次要给大家介绍的是ADO数据库访问技术的使用方法.ADO(Active Data Object,活动数据对象)实际上是一种基于COM(组件对象模型)的自动化接口(IDispatch)技术,并以OLE DB(对象连接和镶入的数据库)为基础

Delphi操作ACCESS技巧集

1.DELPHI中操作access数据库(建立.mdb文件,压缩数据库)以下代码在WIN2K,D6,MDAC2.6下测试通过,编译好的程序在WIN98第二版无ACCESS环境下运行成功.//在之前uses ComObj,ActiveX//声明连接字符串Const SConnectionString = 'PRovider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;' +'Jet OLEDB:Database PassWord=%s;'; //=========

VC++中使用ADO方式操作ACCESS数据库

ADO(ActiveX Data Object)是Microsoft数据库应用程序开发的新接口,是建立在OLE DB之上的高层数据库访问技术,即使你对OLE DB,COM不了解也能轻松对付ADO,因为它非常简单易用,甚至比你以往所接触的ODBC API.DAO.RDO都要容易使用,并不失灵活性.本文详细地介绍在Visual C++开发环境下如何使用ADO来进行数据库应用程序开发,并给出示例代码.为了使读者朋友都能测试本例提供的代码,我们采用Access数据库,您可以直接在我们提供的示例代码中找到

本地通过Eclipse链接Hadoop操作Mysql数据库问题小结

前一段时间,在上一篇博文中描述了自己抽时间在构建的完全分布式Hadoop环境过程中遇到的一些问题以及构建成功后,通过Eclipse操作HDFS的时候遇到的一些问题,最近又想进一步学习学习Hadoop操作Mysql数据库的一些知识,在这里网上存在很多分歧,很多人可能会笑话,用那么“笨重”的Hadoop来操作数据库,脑子有问题吧,Hadoop的HDFS优势在于处理分布式文件系统,这种说法没有任何错误,数据库的操作讲究“安全.轻便.快捷”,用Hadoop操作完全是不符合常理啊,那为啥还要学习这个东西呢