非常实用c#操作Access数据库的例子(application)

上传者: ljdir | 上传时间: 2023-11-17 05:04:12 | 文件大小: 360KB | 文件类型: RAR
本文介绍C#访问操作Access数据库的基础知识,并提供一个相关的例程。 1.通过ADO.NET的OleDb相关类来操作Access 主要知识点如下: using System.Data.OleDb; using System.Data; 连接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb"; 建立连接:OleDbConnection connection = new OleDbConnection(connectionString); 使用OleDbCommand类来执行Sql语句: OleDbCommand cmd = new OleDbCommand(sql, connection); connection.Open(); cmd.ExecuteNonQuery(); 2.取得Access自增标识字段在插入数据后的id值 cmd.CommandText = @"select @@identity"; int value = Int32.Parse(cmd.ExecuteScalar().ToString()); return value; 3.执行事务 需要用到OleDbTransaction,关键语句如下: OleDbConnection connection = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(); OleDbTransaction transaction = null; cmd.Connection = connection; connection.Open(); transaction = connection.BeginTransaction(); cmd.Transaction = transaction; cmd.CommandText=sql1; cmd.ExecuteNonQuery(); cmd.CommandText=sql2; cmd.ExecuteNonQuery(); transaction.Commit(); 4.执行查询,返回DataSet OleDbConnection connection = new OleDbConnection(connectionString); DataSet ds = new DataSet(); connection.Open(); OleDbDataAdapter da = new OleDbDataAdapter(sql, connection); da.Fill(ds,"ds"); 5.分页查询 分页查询使用OleDbDataReader来读取数据,并将结果写到一个DataSet中返回。 以上内容封装为三个可重用的类:AccessDBUtil,AccessPageUtil,Page 代码这里下载AccessDBUtilDemo.rar (191.37 KB , 下载:999次) 本例程是一个c#的winform程序,但是数据访问类可以在Web环境下使用。 本例程演示了: 1.Access数据库的插入,更新,修改,查询; 2.带参数的sql语句的使用,而不是拼SQL; 3.使用DataReader的分页查询,而不是用嵌套的SQL语句来分页; 4.用事务同时执行多个SQL语句; 5.在插入数据的同时返回最新的ID值; 6.整型,实型,字符串,日期型,布尔型五种数据类型的操作; 7.使用正则表达式来验证整数和实数; 8.listview用来显示数据的一些基本用法。 本示例不包括: 1.高效的分页查询,仅仅是提供了一种分页的方法,但我认为DataReader应该比嵌套的SQL语句快(未测试)。 2.完善的分页封装,只提供了分页的简单包装。 3.嵌套的事务处理,提供了同时执行多个sql语句的事务处理,但不支持嵌套事务。 4.listview的使用,只是利用winform控件来演示数据访问,因此不能作为winform编程的良好示例,例如添加数据时界面并没有很好地更新。

文件下载

资源详情

[{"title":"( 33 个子文件 360KB ) 非常实用c#操作Access数据库的例子(application)","children":[{"title":"AccessDBUtilDemo","children":[{"title":"AccessDBUtil.cs <span style='color:#111;'> 6.30KB </span>","children":null,"spread":false},{"title":"AccessPageUtil.cs <span style='color:#111;'> 1.55KB </span>","children":null,"spread":false},{"title":"bin","children":[{"title":"Debug","children":[{"title":"AccessDBUtilDemo.pdb <span style='color:#111;'> 71.50KB </span>","children":null,"spread":false},{"title":"product.mdb <span style='color:#111;'> 828.00KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.exe <span style='color:#111;'> 36.00KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.vshost.exe <span style='color:#111;'> 5.50KB </span>","children":null,"spread":false}],"spread":true},{"title":"Release","children":[{"title":"product.mdb <span style='color:#111;'> 828.00KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.exe <span style='color:#111;'> 32.00KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.vshost.exe <span style='color:#111;'> 5.50KB </span>","children":null,"spread":false},{"title":"readme.txt <span style='color:#111;'> 228B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"obj","children":[{"title":"AccessDBUtilDemo.csproj.FileList.txt <span style='color:#111;'> 496B </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.csproj.FileListAbsolute.txt <span style='color:#111;'> 832B </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"AccessDBUtilDemo.pdb <span style='color:#111;'> 71.50KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.exe <span style='color:#111;'> 36.00KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.MainForm.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"AccessDBUtilDemo.csproj.GenerateResource.Cache <span style='color:#111;'> 772B </span>","children":null,"spread":false}],"spread":true},{"title":"Release","children":[{"title":"AccessDBUtilDemo.exe <span style='color:#111;'> 32.00KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.MainForm.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"AccessDBUtilDemo.csproj.GenerateResource.Cache <span style='color:#111;'> 772B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Page.cs <span style='color:#111;'> 984B </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 636B </span>","children":null,"spread":false},{"title":"Validator.cs <span style='color:#111;'> 764B </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.csproj <span style='color:#111;'> 2.20KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.sln <span style='color:#111;'> 850B </span>","children":null,"spread":false},{"title":"MainForm.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Product.cs <span style='color:#111;'> 863B </span>","children":null,"spread":false},{"title":"PageQueryDao.cs <span style='color:#111;'> 1.72KB </span>","children":null,"spread":false},{"title":"MainForm.cs <span style='color:#111;'> 7.22KB </span>","children":null,"spread":false},{"title":"readme.txt <span style='color:#111;'> 228B </span>","children":null,"spread":false},{"title":"MainForm.Designer.cs <span style='color:#111;'> 13.79KB </span>","children":null,"spread":false},{"title":"ProductDao.cs <span style='color:#111;'> 3.09KB </span>","children":null,"spread":false},{"title":"AccessDBUtilDemo.suo <span style='color:#111;'> 19.00KB </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明