Community discussion forum

Using ADO in C++

This is a comment thread discussing Using ADO in C++
  • 9 years ago

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

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 5 years ago

    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

  • 4 years ago

    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,

  • 4 years ago

    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 mvwin32SQLsrvName1, and mvwin32SQLsrvDB1
    // 1433 is default port
    CString connectionString = "Provider=SQLOLEDB;Data Source=" + mvwin32SQLsrvName1 + ",1433;Network Library=DBMSSOCN;Initial Catalog=" + mvwin32SQLsrvDB1 + ";" + "TrustedConnection=Yes;";
    _ConnectionPtr m
    pConn;
    mpConn.CreateInstance (uuidof(Connection));
    m
    pConn->Open( bstrt( connectionString ), bstrt( "" ), bstrt( "" ), adModeUnknown );//


    // Command ...
    CommandPtr pCommand;
    pCommand.CreateInstance (uuidof (Command));
    pCommand->ActiveConnection = m
    pConn; // Formerly opened connection
    pCommand->CommandText = (bstrt) 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((bstrt) 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/olprappendixa1.asp
    // types of VARENUM, show by typing "VARENUM::" in .NET 2003
    if ((vtFieldValue.vt == VT
    CY) || (vtFieldValue.vt == VTDECIMAL) || (vtFieldValue.vt == VTI1) || (vtFieldValue.vt == VTI2) || (vtFieldValue.vt == VTI4) || (vtFieldValue.vt == VTI8) || (vtFieldValue.vt == VTINT) || (vtFieldValue.vt == VTR4) || (vtFieldValue.vt == VTR8) || (vtFieldValue.vt == VTUI1) || (vtFieldValue.vt == VTUI2) || (vtFieldValue.vt == VTUI4) || (vtFieldValue.vt == VTUI8) || (vtFieldValue.vt == VT_UINT))
    {
        double num = (double) vtFieldValue;
    }
    else
    {
        CString str = (CString) vtFieldValue;
    }


    } // end while

  • 4 years ago

    I think the '\' is used to signify that there is some additional information to do with the current line (similar to macro definitions).

  • 4 years ago

    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

  • 4 years ago

    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();
    }

  • 4 years ago

    This doesn't works on Dev-CPP because it do not support the #import construction...


    JP.

  • 4 years ago

    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.

  • 4 years ago

    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

  • 3 years ago

    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


  • 3 years ago

    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.

  • 3 years ago

    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

  • 3 years ago

  • 3 years ago

    hai,


    can we use register variable for storing address

  • 3 years ago


    yes , it can be used, but in this case , compiler will treat it as auto variable. Compiler will only generate warning.

Post a reply

Enter your message below

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