strSQL.Format(_T("update JPolicy set UpperComCode = '%s' where COMCODE in '%s'"), d_ComCode, d_String);
wchar_t *pSQL = strSQL.AllocSysString();
m_policy1->Open(pSQL, Sp_JiBenFaData.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
完整代码如下:
#include"stdafx.h"
#include
#include
#include
#include
using namespace std;
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
void main()
{
//连接数据库
CoInitialize(NULL); //初始化COM库环境
_ConnectionPtr Sp_JiBenFaData;
_ConnectionPtr Sp_LoginData;
HRESULT hr = Sp_JiBenFaData.CreateInstance(_uuidof(Connection));
HRESULT hr1 = Sp_LoginData.CreateInstance(_uuidof(Connection));
try {
_bstr_t conn_JiBenFaData = "Driver={sql server};server=*;uid=sa;pwd=*;database=JiBenFaData;";
Sp_JiBenFaData->Open(conn_JiBenFaData, "", "", adModeUnknown);
}
catch (_com_error &e) {
cout << e.Description() << endl;
}
try {
_bstr_t conn_LoginData = "Driver={sql server};server=*;uid=sa;pwd=*;database=LoginData;";
Sp_LoginData->Open(conn_LoginData, "", "", adModeUnknown);
}
catch (_com_error &e) {
cout << e.Description() << endl;
}
_RecordsetPtr m_policy1;
_RecordsetPtr m_dcompany;
if (FAILED(m_policy1.CreateInstance(_uuidof(Recordset)))){
cout << "记录集对象指针实例化失败!" << endl;
}
if (FAILED(m_dcompany.CreateInstance(_uuidof(Recordset)))){
cout << "记录集对象指针实例化失败!" << endl;
}
m_dcompany->Open("select * from dbo.ODS_DCOMPANY where UPPERCOMCODE ='35000000'", (IDispatch*)Sp_LoginData, adOpenDynamic, adLockOptimistic, adCmdText);
m_dcompany->MoveFirst();
CString strSQL;
while (!m_dcompany->adoEOF){
string d_ComCode = (char*)(_bstr_t)(m_dcompany->Fields->GetItem(_variant_t("COMCODE"))->Value);
string d_String = (char*)(_bstr_t)(m_dcompany->Fields->GetItem(_variant_t("ComString"))->Value);
cout << d_ComCode << endl;
cout << d_String << endl;
strSQL.Format(_T("update JPolicy set UpperComCode = '%s' where COMCODE in '%s'"), d_ComCode, d_String);
wchar_t *pSQL = strSQL.AllocSysString();
m_policy1->Open(pSQL, Sp_JiBenFaData.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
m_dcompany->MoveNext();
}
system("pause");
}
编译通过,但跳出对话框报错:
Unhandled exception at 0x75E0C42D in test1.exe: Microsoft C++ exception: _com_error at memory location 0x0021F71C.