C++ ado 的疑惑
刚学,写了个例子程序,但是select可以,但是insert、update时就会出现异常,请大家看看是怎么回事。
下面贴的是代码,有异常也可以插入到数据库中去。
#define INITGUID
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include <stdio.h>
#include <icrsint.h>
class CCustomRs : public CADORecordBinding
{
BEGIN_ADO_BINDING(CCustomRs)
ADO_VARIABLE_LENGTH_ENTRY2(1, adVarChar, m_szau_lname,
sizeof(m_szau_lname), lau_lnameStatus, false)
ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_szau_fname,
sizeof(m_szau_fname), lau_fnameStatus, false)
ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_szphone,
sizeof(m_szphone), lphoneStatus, true)
END_ADO_BINDING()public:
CHAR m_szau_lname[41];
ULONG lau_lnameStatus;
CHAR m_szau_fname[41];
ULONG lau_fnameStatus;
CHAR m_szphone[12];
ULONG lphoneStatus;
};
VOID main()
{
IADORecordBinding *picRs = NULL;
::CoInitialize(NULL);
_ConnectionPtr pConn;
pConn.CreateInstance(__uuidof(Connection));
_RecordsetPtr pRs = NULL;
_CommandPtr pCmd = NULL;
CCustomRs rs;
pConn->Open("dsn=hdpu;", "root", "123456", adConnectUnspecified);
pRs.CreateInstance(__uuidof(Recordset));
pCmd.CreateInstance(__uuidof(Command));
pCmd->CommandText = "insert into test values('aaaa','bbbb','cccc');";
pCmd->PutActiveConnection(_variant_t((IDispatch*)pConn));
pRs = pCmd->Execute(NULL,NULL,adCmdText);
pRs->Close();
pConn->Close();
CoUninitialize();
}