一个好用的ODBC数据库类CMYODBC 一、引言 感觉MFC的CRecordset类不是很好用,因为我们要想使用的话必须为每个查询从CRecordset类派生出一个新类,或者进行动态数据交换。在VC知识库第六期上面有一篇介绍"单独使用CRecordset"文章,可是上面的CRecordset打开
一个好用的ODBC数据库类CMYODBC
一、引言
感觉MFC的CRecordset类不是很好用,因为我们要想使用的话必须为每个查询从CRecordset类派生出一个新类,或者进行动态数据交换。在VC知识库第六期上面有一篇介绍"单独使用CRecordset"文章,可是上面的CRecordset打开方式只能使用CRecordset::forwardOnly,游标只能向前滚动,而且用这种方式,你根本无法从打开的记录集中获得本次查询得到了有多少列。有一次在应用的时候,我只好通过捕获CRecordset::GetFieldValue()的异常来得到查询的结果有多少列。为了使用的方便,我自己写了一个数据库类CMYODBC,它是用ODBC API写的,它支持各种sql语句,支持事务处理。它最好的地方在于,对于查询的记录集实现了动态绑定,这是通过类CODBCSet来实现的。要说明的是,香港服务器,这两个类可以说比较简单,两个类的代码量很少,所以建议感兴趣的朋友看一下它的代码,下图是本代码运行效果图:
二、原理
其实无论是使用ODBC API还是使用ORACLE的OCI(对于ORACLE的OCI感兴趣的朋友,欢迎一起探讨,OCI的功能很强大,支持动态绑定,支持pl/sql,它的类的封装和CMYODBC很相似,用它来代替ODBC编程,可以解决ODBC的效率问题)其过程都很相似,一般分为以下几个过程:
1 初始化工作环境
2 连接数据源
3 操作数据源
4 检索结果集
5 更新结果集
6 事务处理
7 断开连接,释放各种句柄
大家都知道在使用CDatabase时候,如果要执行的是select语句的话,那么要通过CRecordset来检索结果集,而CRecordset类要我们先选择表等来先进行绑定,这样我们使用的时候很不方便,其实我们根本不需要这样做,而且我们也不需要知道这次执行的是关于那个数据库那张表的sql语句,因为在执行完SQLExecDirect()后,可以通过调用SQLNumResultCols() 、SQLColAttribute()等函数来获得执行的结果的很多属性,如这次执行的结果集是多少列、每列的字段名、列的类型等,然后根据类型可以动态分配内存,香港服务器租用,然后在用这些内存去绑定,香港服务器,最后能过SQLFetch()来得到结果集。在CMYODBC这个类里是通过CODBCSet类应用上面的原理来实现自动绑定的。
下面介绍一下类CMYODBC的使用方法:
源代码下载
1 通过调用ConnectDB(const char *cpServerName, const char *cpUserName, const char *cpPassword)函数来联接数据库。其中的参数意义如下:
cpServerName-----ODBC数据源名
cpUserName-------用户名
cpPassword-----口令
2 通过调用ExeSqlDirect(const char *cpSqlStmt)函数可以执行一些操作数据源的语句,如修改、删除语句等。其中的参数意义如下: cpSqlStmt------你要执行的sql语句,如delete from emp where deptno
3 如果要执行事务的话,调用ExecTrans(CStringList &strSqlList)函数,其中的参数 strSqlList表示你要执行的一系列sql语句。
4 如果要执行select语句的话,通过下面的步骤:
I 声明一个CODBCSet 对象,如 CODBCSet rSet;
II然后调用函数PrepareSql(const char *cpSql, CODBCSet &rset),其中的参数的意义如下:
cpSql----代表要执行的select语句
rset-----表示一个CODBCSet的引用,你要把上面声明的对象传递进去。
III调用FetchData()函数来取得结果集。
5 通过调用函数DisConnect()断开和数据源的连接。
三、实例练习
下面就通过上面的例子一起来看一下这个类到底怎么样,为了方便,我建了一个简单的access数据库test.mdb,在这个数据库中也只有一张表emp,它有三个字段。在下面的工程的InitInstance ()中,通过代码为它自动建立了一个叫做"daliu"的ODBC数据源。
步骤一:
新建一个基于对话框的工程,命名为demo1,打开stdafx.h文件,加入#include 从例子中把MyODBC.h,MyODBC.cpp, ODBCSet.h, ODBCSet.cpp复制到这个工程的目录下,并且加入到工程中,方法是菜单project->add to project->files,选择这四个文件就可以了。复制test.mdb文件,把它加入到这个工程的debug目录下。也可以是其它的目录,只要和你的执行程序在同一个目录就可以了。
步骤二:
参照上面的对话框,在上面加入按钮和一个ClistCtrl控件,在classwizard上面关联控件的变量ClistCtrl关联m_list控件,为三个EDIT分别关联CString类型的m_strID,m_strName,m_strJob.
步骤三:
在CDemo1App::InitInstance()的最上面,加入下面的代码,实现自动ODBC数据源的增加。
01.char path[MAX_PATH] = {''\0''};

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software