Using ADO from C++

Environment:  Visual C++ 6

Microsoft ActiveX Data Object (ADO) provides an easy way to data access and manipulation that is independent of data stores, tools, and languages. This flexibility and easy-to-code facility makes ADO the perfect choice for developers. ADO is implemented with Component Object Model (COM) interfaces. Unlike VB programmers, C++ programmers must know the details of using COM for using ADO. So, using ADO from C++ is still very complex. But, it is possible to get an easy ADO programming model from C++, which can help to hide the details of using COM. In this article, I demonstrate a C++ class to do this that encapsulates the ADO connection object. You can apply same technique for encapsulating other ADO objects.

Adding ADO Support to a C++ Program

One way to add ADO support to your C++ program is to import ADO type library information. The type library for the current ADO is contained within the ADO DLL, msado15.dll. You can import this type library by importing msado15.dll by using the #import directive:

#import "c:/Program Files/Common Files/System/ADO/msado15.dll"
        rename("EOF", "EndOfFile")

Of course, you may need to change the path of msado15.dll according to the location where the file is found on your machine. Because I don't mention the no_namespace option, the directive generates a type library header (generally msado15.tlh) that contains typedef declarations, smart pointers for interfaces, and enumerators under the ADODB namespace. The rename macro tells the preprocessor to replace the EOF constants by EndOfFile. ADO defines nine objects (Connection, Command, Recordset, Record, Stream, Parameter, Field, Property, and Error) and four collections (Parameters, Fields, Properties, and Errors). The #import directive generates smart pointer-type declarations for all the ADO objects. For example, a variable that points to a _Connection object is of the _ConnectionPtr type. So, if you want to refer to the _Connection object, you have to declare a variable of the _ConnectionPtr type.

ADODB::_ConnectionPtr Cnn;

Now, create a C/C++ header file named .Database.h. and add the codes given below:

#import "c:/Program Files/Common Files/System/ADO/msado15.dll"
        rename("EOF", "EndOfFile")
typedef ADODB::_RecordsetPtr  RecPtr;
typedef ADODB::_ConnectionPtr CnnPtr;

Notice that all typedef declarations, smart pointers for interfaces, and enumerators are in msado15.tlh. The compiler automatically generates this file after compiling the above #import directive. You don't need to include this file because C++ is responsible for this file. Now, we are ready to declare a class, which encapsulates the ADO Connection object. Let's write the code for the class that encapsulates the ADO Connection object.

class Database
{
public:
   CnnPtr m_Cnn;
   Database();
   ~ Database();
   bool Open(char* UserName, char* Pwd, char* CnnStr, char* ErrStr);
   RecPtr Execute(char* CmdStr);
   bool Close();
};

Actually, the ADO Connection object has many methods. But, to simplify the code, I declare only three methods (Open, Close, and Execute) to give access to three methods of the connection object: Open establishes a physical connection to a data source, Execute executes a command or stored procedure on the established connection, and Close breaks the established connection.

About the Author
computer science & engineering, CUET

Go to page: 1  2  Next

Downloads

C++中使用ADO数据库来判断查询表是否存在,可以通过尝试执行一个查询语句来完成。如果查询的表不存在,ADO通常会抛出一个错误。下面是一个简单的例子,展示了如何尝试查询一个表,并通过捕获错误来判断表是否存在: ```cpp #include <iostream> #include <windows.h> #include <adoint.h> using namespace std; // 初始化COM库 void InitializeCOM() { CoInitialize(NULL); } // 清理COM库 void UninitializeCOM() { CoUninitialize(); } // 检查表是否存在 bool TableExists(IDispatch *pConnection, const string &tableName) { bool exists = false; try { // 建立一个记录集指针 _RecordsetPtr pRecordset; // 构造检查表存在的SQL查询语句 string checkQuery = "SELECT * FROM " + tableName + " WHERE 1=0"; // 尝试打开记录集 pRecordset.CreateInstance(__uuidof(Recordset)); pRecordset->Open(checkQuery.c_str(), pConnection, adOpenStatic, adLockOptimistic, adCmdText); // 如果能够成功打开记录集,则表存在 exists = true; } catch (_com_error &e) { // ADO错误发生,检查错误号是否为表不存在的错误 if (e.Error() == 0x800A0E78) { // 错误号为0x800A0E78表示表不存在 exists = false; } else { // 其他错误,根据需要进行处理 cout << "An error occurred: " << e.ErrorMessage() << endl; } } return exists; } int main() { // 初始化COM库 InitializeCOM(); // 创建数据库连接 _ConnectionPtr pConnection; pConnection.CreateInstance(__uuidof(Connection)); // 连接到数据库 try { pConnection->Open("Provider=sqloledb;Data Source=your_server_name;Initial Catalog=your_database_name;User Id=your_username;Password=your_password;", "", "", adConnectUnspecified); } catch (_com_error &e) { cout << "Error connecting to database: " << e.ErrorMessage() << endl; } // 判断表是否存在 string tableName = "your_table_name"; if (TableExists(pConnection, tableName)) { cout << "Table '" << tableName << "' exists." << endl; } else { cout << "Table '" << tableName << "' does not exist." << endl; } // 关闭数据库连接并清理COM库 pConnection->Close(); UninitializeCOM(); return 0; } ``` 注意:上述代码中数据库连接字符串(DSN)、用户名、密码、表名等信息需要根据实际情况进行替换。此代码示例使用了COM库和ADO组件,需要在项目中添加对应的库引用,并确保系统上安装了ADO组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值