C++ ADO封装实现SQLserver参数化的增删查改代码
时间: 2024-03-03 11:49:18 浏览: 215
ADO.NET实现对SQL Server数据库的增删改查示例
好的,下面是一个简单的 C++ ADO 封装实现 SQL Server 参数化的增删查改的代码示例:
```cpp
#include <iostream>
#include <string>
#include <comutil.h> // 需要包含此头文件
#pragma comment(lib, "comsuppw.lib") // 需要链接此库文件
class ADOHelper
{
public:
ADOHelper()
{
::CoInitialize(NULL);
m_conn.CreateInstance("ADODB.Connection");
}
~ADOHelper()
{
if (m_conn != NULL)
{
if (m_conn->State == adStateOpen)
m_conn->Close();
m_conn.Release();
}
::CoUninitialize();
}
bool Open(const std::string& connStr)
{
try
{
m_conn->ConnectionString = _bstr_t(connStr.c_str());
m_conn->Open("", "", "", adConnectUnspecified);
return true;
}
catch (_com_error& e)
{
std::cerr << "Open database error: " << e.ErrorMessage() << std::endl;
return false;
}
}
bool Close()
{
try
{
if (m_conn->State == adStateOpen)
m_conn->Close();
return true;
}
catch (_com_error& e)
{
std::cerr << "Close database error: " << e.ErrorMessage() << std::endl;
return false;
}
}
bool ExecuteNonQuery(const std::string& sql, const std::vector<_variant_t>& parameters)
{
try
{
_CommandPtr cmd;
cmd.CreateInstance("ADODB.Command");
cmd->ActiveConnection = m_conn;
cmd->CommandText = _bstr_t(sql.c_str());
cmd->CommandType = adCmdText;
for (size_t i = 0; i < parameters.size(); ++i)
{
_ParameterPtr param = cmd->CreateParameter("", adVariant, adParamInput, 0, parameters[i]);
cmd->Parameters->Append(param);
}
cmd->Execute(NULL, NULL, adExecuteNoRecords);
return true;
}
catch (_com_error& e)
{
std::cerr << "ExecuteNonQuery error: " << e.ErrorMessage() << std::endl;
return false;
}
}
bool ExecuteScalar(const std::string& sql, const std::vector<_variant_t>& parameters, _variant_t& result)
{
try
{
_CommandPtr cmd;
cmd.CreateInstance("ADODB.Command");
cmd->ActiveConnection = m_conn;
cmd->CommandText = _bstr_t(sql.c_str());
cmd->CommandType = adCmdText;
for (size_t i = 0; i < parameters.size(); ++i)
{
_ParameterPtr param = cmd->CreateParameter("", adVariant, adParamInput, 0, parameters[i]);
cmd->Parameters->Append(param);
}
result = cmd->Execute(NULL, NULL, adCmdText | adExecuteNoRecords);
return true;
}
catch (_com_error& e)
{
std::cerr << "ExecuteScalar error: " << e.ErrorMessage() << std::endl;
return false;
}
}
bool ExecuteReader(const std::string& sql, const std::vector<_variant_t>& parameters, _RecordsetPtr& rs)
{
try
{
_CommandPtr cmd;
cmd.CreateInstance("ADODB.Command");
cmd->ActiveConnection = m_conn;
cmd->CommandText = _bstr_t(sql.c_str());
cmd->CommandType = adCmdText;
for (size_t i = 0; i < parameters.size(); ++i)
{
_ParameterPtr param = cmd->CreateParameter("", adVariant, adParamInput, 0, parameters[i]);
cmd->Parameters->Append(param);
}
rs = cmd->Execute(NULL, NULL, adCmdText);
return true;
}
catch (_com_error& e)
{
std::cerr << "ExecuteReader error: " << e.ErrorMessage() << std::endl;
return false;
}
}
private:
_ConnectionPtr m_conn;
};
int main()
{
ADOHelper ado;
ado.Open("Provider=SQLOLEDB;Data Source=(local);Initial Catalog=TestDB;Integrated Security=SSPI;");
// 插入数据
std::string insertSql = "INSERT INTO [dbo].[Person] ([Name], [Age]) VALUES (?, ?)";
std::vector<_variant_t> insertParams = { _variant_t("John"), _variant_t(30) };
ado.ExecuteNonQuery(insertSql, insertParams);
// 查询数据
std::string selectSql = "SELECT [ID], [Name], [Age] FROM [dbo].[Person] WHERE [Age] >= ?";
std::vector<_variant_t> selectParams = { _variant_t(30) };
_RecordsetPtr rs;
ado.ExecuteReader(selectSql, selectParams, rs);
while (!rs->EndOfFile)
{
std::cout << "ID: " << rs->Fields->Item["ID"]->Value.intVal << ", ";
std::cout << "Name: " << (char*)_bstr_t(rs->Fields->Item["Name"]->Value) << ", ";
std::cout << "Age: " << rs->Fields->Item["Age"]->Value.intVal << std::endl;
rs->MoveNext();
}
// 更新数据
std::string updateSql = "UPDATE [dbo].[Person] SET [Age] = ? WHERE [Name] = ?";
std::vector<_variant_t> updateParams = { _variant_t(35), _variant_t("John") };
ado.ExecuteNonQuery(updateSql, updateParams);
// 删除数据
std::string deleteSql = "DELETE FROM [dbo].[Person] WHERE [ID] = ?";
std::vector<_variant_t> deleteParams = { _variant_t(1) };
ado.ExecuteNonQuery(deleteSql, deleteParams);
ado.Close();
return 0;
}
```
以上代码仅供参考,具体实现要根据自己的需求进行调整。
阅读全文