C++封装ado连接sqlserver增删查改
时间: 2023-07-30 19:08:11 浏览: 210
好的,下面是一个简单的示例代码,演示如何使用C++封装ADO连接SQL Server进行增删查改操作:
```cpp
#include <iostream>
#include <windows.h>
#include <comdef.h>
#include <atlbase.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
class CSQLServer
{
public:
CSQLServer();
~CSQLServer();
bool Connect(const std::string& strServer, const std::string& strDatabase,
const std::string& strUser, const std::string& strPassword);
void Disconnect();
bool ExecuteNonQuery(const std::string& strSQL);
bool ExecuteScalar(const std::string& strSQL, _variant_t& vtResult);
private:
bool InitADO();
void UninitADO();
private:
_ConnectionPtr m_pConnection;
};
CSQLServer::CSQLServer()
{
InitADO();
}
CSQLServer::~CSQLServer()
{
UninitADO();
}
bool CSQLServer::Connect(const std::string& strServer, const std::string& strDatabase,
const std::string& strUser, const std::string& strPassword)
{
try
{
std::string strConnString = "Provider=SQLOLEDB.1;";
strConnString += "Persist Security Info=False;";
strConnString += "Data Source=" + strServer + ";";
strConnString += "Initial Catalog=" + strDatabase + ";";
strConnString += "User ID=" + strUser + ";";
strConnString += "Password=" + strPassword + ";";
m_pConnection->Open(strConnString.c_str(), "", "", adConnectUnspecified);
return true;
}
catch (_com_error& e)
{
std::cout << "Connect failed, error message: " << e.Description() << std::endl;
return false;
}
}
void CSQLServer::Disconnect()
{
m_pConnection->Close();
}
bool CSQLServer::ExecuteNonQuery(const std::string& strSQL)
{
try
{
_RecordsetPtr pRecordset;
pRecordset = m_pConnection->Execute(strSQL.c_str(), NULL, adCmdText);
return true;
}
catch (_com_error& e)
{
std::cout << "ExecuteNonQuery failed, error message: " << e.Description() << std::endl;
return false;
}
}
bool CSQLServer::ExecuteScalar(const std::string& strSQL, _variant_t& vtResult)
{
try
{
_RecordsetPtr pRecordset;
pRecordset = m_pConnection->Execute(strSQL.c_str(), NULL, adCmdText);
if (pRecordset->adoEOF == true)
{
vtResult.Clear();
return false;
}
vtResult = pRecordset->Fields->GetItem((long)0)->Value;
return true;
}
catch (_com_error& e)
{
std::cout << "ExecuteScalar failed, error message: " << e.Description() << std::endl;
return false;
}
}
bool CSQLServer::InitADO()
{
try
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
std::cout << "CoInitialize failed" << std::endl;
return false;
}
hr = m_pConnection.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
std::cout << "CreateInstance failed" << std::endl;
CoUninitialize();
return false;
}
return true;
}
catch (_com_error& e)
{
std::cout << "InitADO failed, error message: " << e.Description() << std::endl;
return false;
}
}
void CSQLServer::UninitADO()
{
m_pConnection.Release();
CoUninitialize();
}
int main()
{
CSQLServer sqlServer;
if (!sqlServer.Connect("localhost", "MyDatabase", "sa", "123456"))
{
return 0;
}
// 插入数据
std::string strSQL = "INSERT INTO MyTable (Name, Age) VALUES ('Tom', 20)";
sqlServer.ExecuteNonQuery(strSQL);
// 修改数据
strSQL = "UPDATE MyTable SET Age = 21 WHERE Name = 'Tom'";
sqlServer.ExecuteNonQuery(strSQL);
// 查询数据
strSQL = "SELECT Age FROM MyTable WHERE Name = 'Tom'";
_variant_t vtResult;
if (sqlServer.ExecuteScalar(strSQL, vtResult))
{
std::cout << "Tom's age is " << (int)vtResult.intVal << std::endl;
}
else
{
std::cout << "Tom not found!" << std::endl;
}
// 删除数据
strSQL = "DELETE FROM MyTable WHERE Name = 'Tom'";
sqlServer.ExecuteNonQuery(strSQL);
sqlServer.Disconnect();
return 0;
}
```
上述代码使用`CSQLServer`类封装了ADO连接SQL Server的操作,并提供了插入、修改、查询、删除数据的方法。在使用时,只需要创建一个`CSQLServer`对象,调用`Connect`方法连接数据库,然后调用相应的方法执行SQL语句即可。
注意:上述代码使用了`#import`指令引入了`msado15.dll`库,需要确保该库已经安装在您的系统中。另外,需要链接`comsupp.lib`库。
阅读全文