我正在使用ado在我的C ++项目中连接SQL Server。
因此,我想插入/更新数据库,并编写了一些查询(请参见代码),并且试图执行连接或命令。
但是它在pConnection->Execute(InsertQuery, NULL, adCmdUnspecified);
处出现错误,显示为Microsoft C++ exception: _com_error at memory location
,并且在调试窗口中有_hr,其值为_hr : DB_E_ERRORSINCOMMAND One or more errors occurred during processing of command. TYPE: HRESULT
我也尝试执行Command,但结果与Connection相同。
我应该将adCmdUnspecified更改为某些内容还是在某个地方出错? 解决办法是什么?预先谢谢你。
//connections above
VARIANT vNameStringType;
_bstr_t InsertQuery(L"Insert Into TestTB(AccountID, Name) Values(@AccountID, @Name)");
hr = pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = pConnection;
pCommand->CommandText = InsertQuery;
pCommand->CommandType = adCmdText;
for (int i = 0; i < size; i++)
{
a[i].name;
a[i].login;
vIntegerType.vt = VT_I4;
vIntegerType.intVal = a[i].login;
vNameStringType.vt = VT_BSTR;
vNameStringType.bstrVal = _bstr_t(a[i].name);
int DataExistCounter = 0;
NameParam = pCommand->CreateParameter(_bstr_t("@Name"), adVarChar, adParamInput, 150, _bstr_t(vNameStringType));
pCommand->Parameters->Append(NameParam);
AccIDParam = pCommand->CreateParameter("@AccountID", adInteger, adParamInput, sizeof(int), vIntegerType);
pCommand->Parameters->Append(AccIDParam);
pRecordset->MoveFirst();
while (!pRecordset->EndOfFile)
{
valFieldNam = pRecordset->Fields->GetItem("Name")->Value;
valFieldAcc = pRecordset->Fields->GetItem("AccountID")->Value;
if (valFieldAcc == a[i].login)
{
DataExistCounter++;
}
pRecordset->MoveNext();
}
if (DataExistCounter > 0)
{
//update
}
else
{
pConnection->Execute(InsertQuery, NULL, adCmdUnspecified);
}
}
pRecordset->Close();
pConnection->Close();
CoUninitialize();