Library tutorials & articles

Using ADO in C++

Inserting & Selecting Records

And inserting a record?

recordset->Open("INSERT INTO mytable VALUES ('Hello')",
    connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
    ADODB::adLockReadOnly, ADODB::adCmdText);

recordset->Open("INSERT INTO mytable VALUES ('Goodbye')",
    connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
    ADODB::adLockReadOnly, ADODB::adCmdText);

Cake. In fact, I insert two records. I guess selecting records is easy too?

recordset->Open("SELECT * from mytable", connection.GetInterfacePtr(),
    ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);

while(!recordset->ADOEOF)
{
    _variant_t var;
    var = recordset->Fields->GetItem(L"value")->GetValue();
    std::cout << static_cast<char *>(_bstr_t(var.bstrVal)) << std::endl;
    recordset->MoveNext();
};

Actually selecting records is bit more difficult in C++, then in Visual Basic. The code to get at the value is a nesting of pointers within pointers. This can be a little complex, but
really it’s easier than the naysayers had told us. Finally, we close the recordset and DROP the table.

recordset->Close();
recordset->Open("DROP TABLE mytable", connection.GetInterfacePtr(),
ADODB::adOpenForwardOnly, ADODB::adLockReadOnly,
ADODB::adCmdText);

That was too easy. Who said ADO in C++ was all that difficult.

Comments

  1. 29 Aug 2005 at 05:38

    yes , it can be used, but in this case , compiler will treat it as auto variable. Compiler will only generate warning.
  2. 15 Jul 2005 at 09:49
    hai,

    can we use register variable for storing address
  3. 10 Jul 2005 at 09:21

  4. 02 May 2005 at 21:48

    Hi,


    Does anyone know how to use C language (not C++) with ADO and/or ODBC againt SQL Server. Any sample or a link to a sample is very much appreciated. Thanks in advance.


    Rass

  5. 24 Apr 2005 at 19:46

    Now show me how to add a new record to the database.   After many hours of working on this, I finally got the program to retrieve the records from the Access database, but I have no idea how to add a new record to the database using ADO.  It just ignores me.

  6. 14 Mar 2005 at 06:37

    Hi,


    The #import directive is used to incorporate information from a type library. The content of the type library is converted into C++ classes, mostly describing the COM interfaces.


    The backslash ('\') symbol is used to include additional lines in a single #import statement. Basically its a line  continuation symbol whenever the single statement breaks in multiple line.


    import "C:\Program files\Common Files\System\Ado\msado15.dll" rename("EOF", "ADOEOF")



    The above line is a single statement having some attributes for the import directives. The rename is one of the attribute for the import directive. Some of the attributes are given below


    exclude implementationonly
    include(…) named
    guids noautoexclude
    noimplementation nonamespace
    rawpropertyprefixes rename
    rename_namespace


  7. 06 Nov 2004 at 00:37

    Can anyone help me figure out how to bind the ADO Recordset to a DataGrid control?
    I'm having problems with any table over two columns.  Any help would be greatly appreciated. Thanks

  8. 24 Jun 2004 at 13:24

    Anyone have any problems with this and winsock?


    It would be really awesome to use this but I am getting some of the following and lots more.


    IncludePath\WinSock2.h(109) : error C2011: 'fdset' : 'struct' type redefinition
           IncludePath\WinSock.h(54) : see declaration of 'fd
    set'
    IncludePath\WinSock2.h(144) : warning C4005: 'FDSET' : macro redefinition
           IncludePath\WinSock.h(88) : see previous definition of 'FD
    SET'
    IncludePath\WinSock2.h(153) : error C2011: 'timeval' : 'struct' type redefinition
           IncludePath\WinSock.h(97) : see declaration of 'timeval'
    IncludePath\WinSock2.h(209) : error C2011: 'hostent' : 'struct' type redefinition
           IncludePath\WinSock.h(153) : see declaration of 'hostent'
    IncludePath\WinSock2.h(222) : error C2011: 'netent' : 'struct' type redefinition
           IncludePath\WinSock.h(166) : see declaration of 'netent'
    IncludePath\WinSock2.h(229) : error C2011: 'servent' : 'struct' type redefinition
          IncludePath\WinSock.h(173) : see declaration of 'servent'
    IncludePath\WinSock2.h(241) : error C2011: 'protoent' : 'struct' type redefinition
           IncludePath\WinSock.h(185) : see declaration of 'protoent'
    IncludePath\WinSock2.h(397) : error C2011: 'sockaddr_in' : 'struct' type


    I shortened the include path from c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include to IncludePath to make it more readable.


    I originally had the same problem as the others with VS .NET 2003 without the \ at the end of the #import, now I am getting this.


    If I instead do the #import after the thing I have that includes winsock I get


    ProjectPath\msado15.tlh(893) : error C2146: syntax error : missing ';' before identifier 'NewEnum'
    ProjectPath\msado15.tlh(893) : error C2501: 'ADODB::
    Collection::IUnknownPtr' : missing storage-class or type specifiers
    ProjectPath\msado15.tlh(893) : warning C4183: '_NewEnum': missing return type; assumed to be a member function returning 'int'
    ProjectPath\msado15.tlh(1136) : error C2146: syntax error : missing ';' before identifier 'DataFormat'
    ProjectPath\msado15.tlh(1136) : error C2501: 'ADODB::Field20::IUnknownPtr' : missing storage-class or type specifiers
    ProjectPath\msado15.tlh(1136) : error C2501: 'ADODB::Field20:ataFormat' : missing storage-class or type specifiers
    ProjectPath\msado15.tlh(1158) : error C2146: syntax error : missing ';' before identifier 'GetDataFormat'


    ...basically it has no idea what a IUnkownPtr is and im not sure why.


    As a side note, the article is very cool and looks to be very promising, much easier/flexible than what I was using.

  9. 15 Apr 2004 at 07:11
    This doesn't works on Dev-CPP because it do not support the #import construction...

    JP.
  10. 25 Mar 2004 at 07:47

    I made some changes... maybe now it works in Dev CPP


    include <iostream.h>


    import "C:\Program files\Common Files\System\Ado\msado15.dll" rename("EOF", "ADOEOF")



    void main()
    {
       HRESULT hr;
       CoInitialize(NULL);
       try
       {
           ADODB::ConnectionPtr conn;
           hr = conn.CreateInstance(uuidof(ADODB::Connection));
           if (FAILED(hr))
           {
               throw _com
    error(hr);
           }
           ADODB::RecordsetPtr rs;
           hr = rs.CreateInstance(
    uuidof(ADODB::Recordset));
           if (FAILED(hr))
           {
               throw _com
    error(hr);
           }
       
           conn->Open(L"Data Source=Databasename;User Id=username;Password=password;", L"",
           L"",-1);
           
           rs->Open("select * from table",
           conn.GetInterfacePtr(),
           ADODB::adOpenForwardOnly,
           ADODB::adLockReadOnly,
           ADODB::adCmdText);


           while(!rs->ADOEOF)
           {
               variantt var;
               var = rs->Fields->GetItem(L"columname")->GetValue();
               cout << _bstr
    t(var.bstrVal)  << endl;
               rs->MoveNext();
           };
           rs->Close();
       }
       catch(comerror &e)
       {
           cout << e.Description();
       }
       catch(...)
       {
           cout << "Unhandled Exception";
       };
       CoUninitialize();
    }

  11. 23 Feb 2004 at 10:18
    I am the other one asking on how to use ADO from C++. The way you describe works in VC++.
    I am using Dev Cpp (from www.bloodshed.net) and it does not seem to support this way.
    Any ideas & suggestions are welcome. Thanx Leos
  12. 14 Jan 2004 at 06:24
    I think the '\' is used to signify that there is some additional information to do with the current line (similar to macro definitions).
  13. 13 Jan 2004 at 16:04
    Yes, you are correct.  The bigger complexity is why the "\"?

    By the way, if working with OleDb types in MS ADO / MFC C++,
    this little snippet may be of use to you (tested on XP and Win2000)

    // Connection ...
    // variables here are mvwin32SQLsrvName_1, and mvwin32SQLsrvDB_1
    // 1433 is default port
    CString connectionString = "Provider=SQLOLEDB;Data Source=" + mvwin32SQLsrvName_1 + ",1433;Network Library=DBMSSOCN;Initial Catalog=" + mvwin32SQLsrvDB_1 + ";" + "Trusted_Connection=Yes;";
    _ConnectionPtr m_pConn;
    m_pConn.CreateInstance (__uuidof(Connection));
    m_pConn->Open( _bstr_t( connectionString ), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown );//

    // Command ...
    _CommandPtr pCommand;
    pCommand.CreateInstance (__uuidof (Command));
    pCommand->ActiveConnection = m_pConn; // Formerly opened connection
    pCommand->CommandText = (_bstr_t) chosenQuery;
    // Recordset ...
    _RecordsetPtr pRecordset;
    pRecordset.CreateInstance (__uuidof (Recordset));
    pRecordset->CursorLocation = adUseClient;
    pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown);
       
    while (!pRecordset->GetadoEOF())
    {
    variant_t vtFieldValue;
    vtFieldValue = pRecordset->Fields->GetItem((_bstr_t) column)->Value;

    // all numeric types verified by Microsoft's OLE DB Programmer's Reference
    // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/oledb/htm/olprappendixa_1.asp
    // types of VARENUM, show by typing "VARENUM::" in .NET 2003
    if ((vtFieldValue.vt == VT_CY) || (vtFieldValue.vt == VT_DECIMAL) || (vtFieldValue.vt == VT_I1) || (vtFieldValue.vt == VT_I2) || (vtFieldValue.vt == VT_I4) || (vtFieldValue.vt == VT_I8) || (vtFieldValue.vt == VT_INT) || (vtFieldValue.vt == VT_R4) || (vtFieldValue.vt == VT_R8) || (vtFieldValue.vt == VT_UI1) || (vtFieldValue.vt == VT_UI2) || (vtFieldValue.vt == VT_UI4) || (vtFieldValue.vt == VT_UI8) || (vtFieldValue.vt == VT_UINT))
    {
        double num = (double) vtFieldValue;
    }
    else
    {
        CString str = (CString) vtFieldValue;
    }

    } // end while
  14. 13 Jan 2004 at 11:01
    Hi,

    I've just had the same problem, I solved it with these steps:

    1) move
    #import "C:\Program files\Common Files\System\Ado\msado15.dll" \
    rename("EOF", "ADOEOF")

    to the stdafx.h file (note that I've added a '\' at the end of the first line.

    2) make sure #include "stdafx.h" appears before any #includes in your main file.

    hopefully that will then compile.
    Andy,
  15. 21 Oct 2003 at 17:37

    I'm using Visual Studio .NET 2003, and when I place the statements


    import "C:\Program files\Common Files\System\Ado\msado15.dll"


    rename("EOF", "ADOEOF")


    into a header file, and include the header in a sample project with no compilation or linkage errors,
    I get the following errors:


    Notice the rename errors, I thought that was ok?


    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\Debug\msado15.tlh(2375): error C2059: syntax error : '-'
    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\dataDlg.h(5): error C2078: too many initializers
    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\dataDlg.h(5): error C2143: syntax error : missing ';' before '<class-head>'
    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\Debug\msado15.tlh(2375): error C2238: unexpected token(s) preceding ';'
    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\dataDlg.h(5): error C2365: 'rename' : redefinition; previous definition was a 'function'
    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\dataDlg.h(5): error C2440: 'initializing' : cannot convert from 'const char [7]' to 'int'
    c:\Documents and Settings\Dan\My Documents\Visual Studio Projects\ADOEx\dataDlg.h(5): error C2501: 'rename' : missing storage-class or type specifiers

  16. 01 Jan 1999 at 00:00

    This thread is for discussions of Using ADO in C++.

Leave a comment

Sign in or Join us (it's free).

Randy Charles Morin Randy's article are Copyright 1998-2003 Randy Charles Morin

We'd love to hear what you think! Submit ideas or give us feedback