C++ ADO封装连接sqlserver数据库的头文件、源文件和主函数调用实现增删查改
时间: 2024-01-30 22:02:02 浏览: 115
Example_DataBase_ADO_Controle.txt
以下是一个简单的C++ ADO封装连接sqlserver数据库的示例代码,包含连接、增删查改等功能:
头文件:
```c++
#pragma once
#include <iostream>
#include <windows.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
class CADOConn {
public:
CADOConn();
~CADOConn();
bool Open(const std::wstring& strConn);
void Close();
bool ExecuteSQL(const std::wstring& strSQL);
bool ExecuteSQL(const std::wstring& strSQL, _RecordsetPtr& pRs);
bool Add(const std::wstring& strSQL);
bool Delete(const std::wstring& strSQL);
bool Update(const std::wstring& strSQL);
bool Select(const std::wstring& strSQL, _RecordsetPtr& pRs);
private:
_ConnectionPtr m_pConn;
};
```
源文件:
```c++
#include "ADOConn.h"
CADOConn::CADOConn()
: m_pConn(nullptr)
{
CoInitialize(nullptr);
}
CADOConn::~CADOConn()
{
Close();
CoUninitialize();
}
bool CADOConn::Open(const std::wstring& strConn)
{
try {
if (m_pConn != nullptr) {
Close();
}
HRESULT hr = m_pConn.CreateInstance(__uuidof(Connection));
if (FAILED(hr)) {
throw _com_error(hr);
}
hr = m_pConn->Open(strConn.c_str(), L"", L"", adModeUnknown);
if (FAILED(hr)) {
throw _com_error(hr);
}
}
catch (_com_error e) {
std::cerr << "Error: " << e.Description() << std::endl;
return false;
}
return true;
}
void CADOConn::Close()
{
if (m_pConn != nullptr) {
m_pConn->Close();
m_pConn.Release();
}
}
bool CADOConn::ExecuteSQL(const std::wstring& strSQL)
{
try {
if (m_pConn == nullptr) {
return false;
}
HRESULT hr = m_pConn->Execute(strSQL.c_str(), nullptr, adCmdText);
if (FAILED(hr)) {
throw _com_error(hr);
}
}
catch (_com_error e) {
std::cerr << "Error: " << e.Description() << std::endl;
return false;
}
return true;
}
bool CADOConn::ExecuteSQL(const std::wstring& strSQL, _RecordsetPtr& pRs)
{
try {
if (m_pConn == nullptr) {
return false;
}
pRs.CreateInstance(__uuidof(Recordset));
HRESULT hr = pRs->Open(strSQL.c_str(), m_pConn.GetInterfacePtr(), adOpenStatic, adLockReadOnly, adCmdText);
if (FAILED(hr)) {
throw _com_error(hr);
}
}
catch (_com_error e) {
std::cerr << "Error: " << e.Description() << std::endl;
return false;
}
return true;
}
bool CADOConn::Add(const std::wstring& strSQL)
{
return ExecuteSQL(strSQL);
}
bool CADOConn::Delete(const std::wstring& strSQL)
{
return ExecuteSQL(strSQL);
}
bool CADOConn::Update(const std::wstring& strSQL)
{
return ExecuteSQL(strSQL);
}
bool CADOConn::Select(const std::wstring& strSQL, _RecordsetPtr& pRs)
{
return ExecuteSQL(strSQL, pRs);
}
```
主函数调用:
```c++
int main() {
// 建立连接
CADOConn conn;
std::wstring strConn = L"Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDB;User Id=sa;Password=123456;";
if (!conn.Open(strConn)) {
std::cerr << "Failed to connect to database!" << std::endl;
return -1;
}
// 插入数据
std::wstring strSQL = L"INSERT INTO MyTable (ID, Name) VALUES (1, 'Tom')";
if (!conn.Add(strSQL)) {
std::cerr << "Failed to insert data!" << std::endl;
}
// 查询数据
strSQL = L"SELECT * FROM MyTable";
_RecordsetPtr pRs;
if (conn.Select(strSQL, pRs)) {
while (!pRs->adoEOF) {
std::wcout << pRs->Fields->GetItem(L"ID")->Value.intVal << L"\t"
<< pRs->Fields->GetItem(L"Name")->Value.bstrVal << std::endl;
pRs->MoveNext();
}
}
else {
std::cerr << "Failed to select data!" << std::endl;
}
// 更新数据
strSQL = L"UPDATE MyTable SET Name='Jerry' WHERE ID=1";
if (!conn.Update(strSQL)) {
std::cerr << "Failed to update data!" << std::endl;
}
// 删除数据
strSQL = L"DELETE FROM MyTable WHERE ID=1";
if (!conn.Delete(strSQL)) {
std::cerr << "Failed to delete data!" << std::endl;
}
// 关闭连接
conn.Close();
return 0;
}
```
以上是一个简单的C++ ADO封装连接sqlserver数据库的示例代码,可以实现基本的增删查改等功能,具体的SQL语句需要根据实际情况进行调整。
阅读全文