c++ sqlserver 增删改查
时间: 2023-06-06 19:01:20 浏览: 401
SQL Server是一种常用的关系型数据库管理系统,支持增删改查等基本操作。
增加数据:使用INSERT语句将数据插入到数据库表中。使用INSERT INTO语句,指定要插入到哪个表以及要插入的值。INSERT INTO tablename (column1, column2, column3) VALUES (value1, value2, value3)。
修改数据:使用UPDATE语句修改数据库表中的数据。使用UPDATE语句,指定表名、要修改的列和新的值,以及选择要修改的行。UPDATE tablename SET column1=value1, column2=value2 WHERE ...。
删除数据:使用DELETE语句删除表中的数据。使用DELETE语句,指定要删除哪个表以及要删除哪些行。DELETE FROM tablename WHERE ...。
查询数据:使用SELECT语句从数据库表中检索数据。使用SELECT语句,指定要检索哪个表、要检索哪些列以及需要检索哪些行。SELECT column1, column2 FROM tablename WHERE ...。
通过这四个基本操作,我们可以对SQL Server数据库进行灵活的控制和管理,方便地操作数据。在实际开发和应用中需要根据实际需求,使用不同的SQL语句和操作,来完成数据处理和管理任务。
相关问题
C++封装ado连接sqlserver增删查改
好的,下面是一个简单的示例代码,演示如何使用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`库。
oracle 的oci 将他封装成改一个C++类,增删改查,代码实现
以下是一个简单的封装OCI的C++类,实现对Oracle数据库的增删改查操作。
```c++
#include <iostream>
#include <string>
#include <oci.h>
class OracleDB {
public:
OracleDB() : envhp(nullptr), errhp(nullptr), srvhp(nullptr), svchp(nullptr), stmthp(nullptr) {}
~OracleDB() {
if (envhp) OCIHandleFree(envhp, OCI_HTYPE_ENV);
if (errhp) OCIHandleFree(errhp, OCI_HTYPE_ERROR);
if (srvhp) OCIHandleFree(srvhp, OCI_HTYPE_SERVER);
if (svchp) OCIHandleFree(svchp, OCI_HTYPE_SVCCTX);
if (stmthp) OCIHandleFree(stmthp, OCI_HTYPE_STMT);
}
bool connect(const std::string& user, const std::string& password, const std::string& db) {
OCIEnvCreate(&envhp, OCI_THREADED, nullptr, nullptr, nullptr, nullptr, 0, nullptr);
OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, nullptr);
OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, nullptr);
OCIHandleAlloc(envhp, (void**)&svchp, OCI_HTYPE_SVCCTX, 0, nullptr);
OCILogon2(envhp, errhp, &svchp, (const OraText*)user.c_str(), user.length(), (const OraText*)password.c_str(), password.length(), (const OraText*)db.c_str(), db.length(), OCI_DEFAULT);
return true;
}
void disconnect() {
OCILogoff(svchp, errhp);
}
bool execute(const std::string& sql) {
OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, nullptr);
OCIStmtPrepare(stmthp, errhp, (const OraText*)sql.c_str(), sql.length(), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, 1, 0, nullptr, nullptr, OCI_DEFAULT);
return true;
}
int query(const std::string& sql) {
OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, nullptr);
OCIStmtPrepare(stmthp, errhp, (const OraText*)sql.c_str(), sql.length(), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, 0, 0, nullptr, nullptr, OCI_DEFAULT);
int cols = 0;
OCIAttrGet(stmthp, OCI_HTYPE_STMT, &cols, nullptr, OCI_ATTR_PARAM_COUNT, errhp);
return cols;
}
private:
OCIEnv* envhp;
OCIError* errhp;
OCIServer* srvhp;
OCISvcCtx* svchp;
OCIStmt* stmthp;
};
int main() {
OracleDB db;
db.connect("user", "password", "db");
// 插入数据
db.execute("INSERT INTO table_name(col1, col2) VALUES('value1', 'value2')");
// 更新数据
db.execute("UPDATE table_name SET col1='new_value1' WHERE col2='value2'");
// 删除数据
db.execute("DELETE FROM table_name WHERE col1='new_value1'");
// 查询数据
int cols = db.query("SELECT * FROM table_name WHERE col2='value2'");
std::cout << "Number of columns: " << cols << std::endl;
db.disconnect();
return 0;
}
```
这个类中,connect函数用于连接数据库;disconnect函数用于断开连接;execute函数用于执行insert、update、delete等操作;query函数用于执行select操作,并返回结果列数。这只是一个简单的实现,具体的实现方式可以根据实际需要进行调整。
阅读全文
相关推荐

















