代码

#include <iostream>
#include <stdlib.h>
#include <string>
#include <windows.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")

int main() {

	CoInitialize(NULL);
	_ConnectionPtr pConnection("ADODB.Connection");
	_bstr_t strConnect = "Provider=SQLOLEDB;Data Source=192.168.10.144;Initial Catalog=SZTD-HEMS;User ID=sa;Password=cvos@2023";

	try {
		pConnection->Open(strConnect, "", "", adConnectUnspecified); //打开连接

		if (pConnection->State)
		{
			std::cout << "Database connected successfully!" << std::endl;
			//执行SQL查询
			_bstr_t strSQL = "INSERT INTO CS_EveEventInfo (id, deviceId, deviceName, eventCode, eventSource, eventSourceName, eventSourceSub, eventSourceSubName, levelCode,levelName, sourceName, title, contentText, dateTime, reamrks, hasVideo, hasImage, deviceTypeCode, deviceTypeName, classifyNumber, classifyName, leftRightFlag, leftRightFlagName, locationParentIds, locationParentNames, pileNumber, pileNumberStr, locationId, locationName, roadSectionId, roadSectionName, roadId, roadName, sysEventcategoryoid, subtypeid, menutype, image, video, imageUrl, video_streaming_front_url, video_streaming_rear_url) VALUES('184375464999999999', 'RDTV-SC-TU-L-0001BO', '雷视拟合设备', '900', 'sense', '事件侦测', NULL, NULL, NULL, NULL, NULL,'低速', '低速', '2023-12-06 19:02:41.000', NULL, 0, 0, 'radarvideo', '雷视拟合设备', 'radar', '雷达', 1, '深圳方向','d8dbe5c1-8a94-c0f3-17a0-47c6c0f28d76,3fd4607c-f1d1-b6e6-8194-3f88cba0f68f,252b14b5-b605-1d46-2ac6-71e3c0797cfc',	'G2518深圳—岑溪高速公路>深中通道>水下隧道', 6095, 'K6+095.0', '252b14b5-b605-1d46-2ac6-71e3c0797cfc', '水下隧道','3fd4607c-f1d1-b6e6-8194-3f88cba0f68f', '深中通道', 'd8dbe5c1-8a94-c0f3-17a0-47c6c0f28d76','G2518深圳—岑溪高速公路', 'c4d5430b-2474-4ace-8599-586e49355575', '3f245d57-b90a-4646-a557-55caf56b0ea6', 1, NULL, NULL, NULL,'rtsp://admin:admin123@192.168.3.202:554/cam/realmonitor?channel=1&subtype=0', 'rtsp://admin:@192.168.3.245:554/dwtype=0&protocol=onvif')";
			_RecordsetPtr pRecordset;
			pRecordset = pConnection->Execute(strSQL, NULL, adCmdText); //执行查询并将结果存储在Recordset对象中
			//while (!pRecordset->EndOfFile) { //遍历Recordset对象中的每一行数据
			//	;
			//	//pRecordset->Fields->GetItem(0)->Value;
			//	std::string strMsg;
			//	strMsg = (_bstr_t)pRecordset->GetCollect("id");
			//	//std::cout << strMsg << std::endl; //输出第一列的值(可以根据需要输出其他列的值)
			//	std::cout << strMsg << std::endl;
			//	pRecordset->MoveNext(); //移动到下一行数据
			//}
		}
		else
			std::cout << "Failed to connect to database." << std::endl;
	}
	catch (_com_error e) {
		std::cout << "Error: " << e.Description() << std::endl; //捕捉异常
	}

	pConnection->Close(); //关闭数据库连接

	CoUninitialize(); //释放COM库

	system("pause");
	return 0;
}