Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
=============================================
_bstr_t strName;
_bstr_t strAge;
ADODB::_ConnectionPtr pConn("ADODB.Connection");
hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
strName = "'Codersource C++ ADO insert Sample',";
strAge = "10,";
_bstr_t insert = "INSERT INTO Users (Username, Phonenumber)"  & "_values('"&strName&"','"strAge"')";
	  
//insert +=strName+strAge;
    
ADODB::_RecordsetPtr pRS("ADODB.Recordset");
hr = pRS->Open(insert,_variant_t((IDispatch *) pConn, true),
    ADODB::adOpenUnspecified,
    ADODB::adLockUnspecified,
    ADODB::adCmdText);


i googled and i got some way to inserting value from the keyboard but it doesn't work
Posted
Updated 7-May-12 2:07am
v4
Comments
Jochen Arndt 7-May-12 8:00am    
Added formatting and copied text from solution. Please use the 'Improve question' link to update your question.
CPallini 7-May-12 8:02am    
'is not working' is a really poor diagnostic message. I bet your compiler is giving more info (hint: check hr return value)

1 solution

The code is not working. To execute SQL commands, use something like this:

C++
ADODB::_ConnectionPtr pConn = NULL;  
ADODB::_CommandPtr pCmd = NULL;  

HRESULT hr = pConn.CreateInstance(__uuidof(ADODB::Connection));
hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
hr = pCmd.CreateInstance(__uuidof( ADODB::Command));
pCmd->ActiveConnection = pConn;  
pCmd->CommandText = insert;  
pCmd->Execute(NULL, NULL, ADODB::adCmdText);
pConn->Close();


To use recordsets:

C++
ADODB::_RecordsetPtr pRS = NULL;  
hr = pRS.CreateInstance(__uuidof(ADODB::Recordset));
pRS->Open(
    _T("Users"),                           // source
    _variant_t((IDispatch *)pConn, true),  // active connection
    ADODB::adOpenKeyset,                   // cursor type
    ADODB::adLockOptimistic,               // lock type
    ADODB::adCmdTable);                    // source is table name
pRS->AddNew();
pRS->Fields->GetItem(_variant_t(_T("UserName")))->Value = _variant_t(strName);
pRS->Fields->GetItem(_variant_t(_T("PhoneNumber")))->Value = _variant_t(strAge);
pRS->Update();
pRS->Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900