直接通过DAO读写Access文件
直接 利用DAO来创建、读写Access 文件 ,总的说来,对比上篇《 直接 通过 ODBC读、写Excel 文件 》来讲,要简单一些。在下面的示例中,我们将用到两种方法:SQL和DAO类函数来混合实现它们,这样做的目地,我想可以使大家更加方便灵活的运用它们来完成你想要
直接利用DAO来创建、读写Access文件,总的说来,对比上篇《直接通过ODBC读、写Excel文件》来讲,要简单一些。在下面的示例中,我们将用到两种方法:SQL和DAO类函数来混合实现它们,这样做的目地,我想可以使大家更加方便灵活的运用它们来完成你想要做的东西。在示例程序中默认指定创建数据库名为:Demo.mdb,内部表名为:DemoTable,写入两个字段:名字和年龄,采用和上一篇读写Excel类似的操作,你也可以根据自己需要来动态改变它们。示例程序运行界面如下所示:

下面让我们来简要看看它的实现步骤:
1. 首先,应确保包含进了afxdao.h头文件,可以在StdAfx.h文件中包含它,如下:
#include <afxdao.h> //加入DAO数据库支持</afxdao.h>
2. 声明DAO库及其记录集变量,可在你的实现文件中加入下面代码: CDaoDatabase db; //数据库<br>CDaoRecordset RecSet(&db); //记录集
3. 接着,先让我们来实现它的创建及写入操作void CRWAccessDlg::OnWriteAccess()<br>{<br> //获取主程序所在路径,存在sPath中<br> CString sPath;<br> GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);<br> sPath.ReleaseBuffer ();<br> int nPos;<br> nPos=sPath.ReverseFind (''\\'');<br> sPath=sPath.Left (nPos);<br> //默认创建数据名:Demo.mdb,内部表名:DemoTable,表内有二个字段:姓名、年龄<br> CString lpszFile = sPath + "\\Demo.mdb";<br> <br> CFileFind fFind;<br> BOOL bSuccess;<br> bSuccess=fFind.FindFile(lpszFile);<br> fFind.Close ();<br> //是否已有创建好的Demo.mdb<strong>文件</strong>,没有则创建它<br> if(!bSuccess)<br> {<br> db.Create(lpszFile);<br> CString SqlCmd = "CREATE TABLE DemoTable(Name VARCHAR(20),Age VARCHAR(3));";<br> db.Execute(SqlCmd);<br> <br> //打开已创建的数据表<br> RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,<br> "SELECT * FROM DemoTable", 0);<br> //加入第一个记录,用SQL语句<br> db.Execute("INSERT INTO DemoTable (Name,Age) VALUES (''徐景周'',26)");<br> <br> //加入第二个记录,用DAO涵数<br> RecSet.AddNew();<br> RecSet.SetFieldValue("Name","徐志慧");<br> RecSet.SetFieldValue("Age","21");<br> RecSet.Update();<br> <br> //加入第三个记录,用DAO涵数<br> RecSet.AddNew();<br> RecSet.SetFieldValue("Name","郭徽");<br> RecSet.SetFieldValue("Age","27");<br> RecSet.Update();<br> <br> //关闭记录集及库<br> RecSet.Close();<br> db.Close();<br> AfxMessageBox("Access<strong>文件</strong>写入成功!");<br> }<br> else<br> AfxMessageBox("Demo.mdb数据库已经创建!");<br> <br>}
4. 最后,让我们来实现它的读取操作。void CRWAccessDlg::OnReadAccess()<br>{<br> COleVariant var; // 字段类型<br> var.ChangeType(VT_BSTR, NULL);<br> CString strName,strAge,strFile;<br> //清空列表框<br> m_AccessList.ResetContent();<br> //获取主程序所在路径,存在sPath中<br> CString sPath;<br> GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);<br> sPath.ReleaseBuffer ();<br> int nPos;<br> nPos=sPath.ReverseFind (''\\'');<br> sPath=sPath.Left (nPos);<br> strFile = sPath + "\\demo.mdb";<br> db.Open(strFile); // 打开已创建的demo数据库及DamoTable表<br> RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT * FROM DemoTable",NULL);<br> while(!RecSet.IsEOF()) // 有没有到表结尾<br> {<br> RecSet.GetFieldValue("Name",var);<br> strName = (LPCSTR)var.pbstrVal;<br> RecSet.GetFieldValue("Age",var);<br> strAge = (LPCSTR)var.pbstrVal;<br> m_AccessList.AddString( strName + " --> "+strAge );<br> RecSet.MoveNext();<br> }<br> //关闭记录集及库<br> RecSet.Close();<br> db.Close();<br>}
以上部分代码的具体实现的细节问题,可在下载实例代码后,仔细查看源码既可(内有详细注释)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. Open settings in Windows 11. You can use Win+I shortcut or any other method. 2. Go to the Apps section and click Apps & Features. 3. Find the application you want to prevent from running in the background. Click the three-dot button and select Advanced Options. 4. Find the [Background Application Permissions] section and select the desired value. By default, Windows 11 sets power optimization mode. It allows Windows to manage how applications work in the background. For example, once you enable battery saver mode to preserve battery, the system will automatically close all apps. 5. Select [Never] to prevent the application from running in the background. Please note that if you notice that the program is not sending you notifications, failing to update data, etc., you can

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

DAO (Data Access Object) in Java is used to separate application code and persistence layer, its advantages include: Separation: Independent from application logic, making it easier to modify it. Encapsulation: Hide database access details and simplify interaction with the database. Scalability: Easily expandable to support new databases or persistence technologies. With DAOs, applications can call methods to perform database operations such as create, read, update, and delete entities without directly dealing with database details.

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

In Java, a "field" is a data member in a class or interface that is used to store data or state. The properties of field include: type (can be any Java data type), access rights, static (belongs to a class rather than an instance), final (immutable) and transient (not serialized). Field is used to store state information of a class or interface, such as storing object data and maintaining object state.

The Java reflection mechanism allows programs to dynamically modify the behavior of classes without modifying the source code. By operating the Class object, you can create instances through newInstance(), modify private field values, call private methods, etc. Reflection should be used with caution, however, as it can cause unexpected behavior and security issues, and has a performance overhead.

Ways to solve iframe cross-domain issues in Vue: CORS: Enable CORS support in the backend server and use XMLHttpRequest or fetch API to send CORS requests in Vue. JSONP: Dynamically load JSONP scripts in Vue using the JSONP endpoint in the backend server. Proxy server: Set up a proxy server to forward requests, use a third-party library (such as axios) in Vue to send requests and set the proxy server URL.

Common exception types and their repair measures in Java function development During the development of Java functions, various exceptions may be encountered, which affect the correct execution of the function. The following are common exception types and their repair measures: 1. NullPointerException Description: Thrown when accessing an object that has not been initialized. Fix: Make sure you check the object for non-null before using it. Sample code: try{Stringname=null;System.out.println(name.length());}catch(NullPointerExceptione){
