Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[4033] Using ADO in C++

Last post 08-29-2005 5:38 AM by souvik. 15 replies.
Page 1 of 2 (16 items) 1 2 Next >
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4033] Using ADO in C++

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

    • Post Points: 0
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 10-21-2003 5:37 PM In reply to

    • dotBomb
    • Not Ranked
    • Joined on 10-21-2003
    • New Member
    • Points 20

    errors on import of the ADO DLL, does this even wo

    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
    • Post Points: 0
  • 01-13-2004 11:01 AM In reply to

    solution to problem

    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,
    • Post Points: 0
  • 01-13-2004 4:04 PM In reply to

    • dotBomb
    • Not Ranked
    • Joined on 10-21-2003
    • New Member
    • Points 20

    Thank you and more

    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
    • Post Points: 0
  • 01-14-2004 6:24 AM In reply to

    use of '\'

    I think the '\' is used to signify that there is some additional information to do with the current line (similar to macro definitions).
    • Post Points: 0
  • 02-23-2004 10:18 AM In reply to

    • Leos
    • Not Ranked
    • Joined on 02-23-2004
    • New Member
    • Points 15

    ADO from Dev Cpp ???

    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
    • Post Points: 0
  • 03-25-2004 7:47 AM In reply to

    • tenno
    • Not Ranked
    • Joined on 03-25-2004
    • New Member
    • Points 5
    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)
           {
               _variant_t var;
               var = rs->Fields->GetItem(L"colum_name")->GetValue();
               cout << _bstr_t(var.bstrVal)  << endl;
               rs->MoveNext();
           };
           rs->Close();
       }
       catch(_com_error &e)
       {
           cout << e.Description();
       }
       catch(...)
       {
           cout << "Unhandled Exception";
       };
       CoUninitialize();
    }
    • Post Points: 0
  • 04-15-2004 7:11 AM In reply to

    • jpfarias
    • Not Ranked
    • Joined on 04-15-2004
    • New Member
    • Points 5
    This doesn't works on Dev-CPP because it do not support the #import construction...

    JP.
    • Post Points: 0
  • 06-24-2004 1:24 PM In reply to

    • dclyde
    • Not Ranked
    • Joined on 06-24-2004
    • New Member
    • Points 5

    Collision with winsock?

    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: 'fd_set' : 'struct' type redefinition
           IncludePath\WinSock.h(54) : see declaration of 'fd_set'
    IncludePath\WinSock2.h(144) : warning C4005: 'FD_SET' : 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.
    • Post Points: 0
  • 11-06-2004 12:37 AM In reply to

    • jensbr00
    • Not Ranked
    • Joined on 11-06-2004
    • New Member
    • Points 5

    Bind to a DataGrid

    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
    • Post Points: 0
  • 03-14-2005 6:37 AM In reply to

    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 implementation_only
    include(…) named_guids no_auto_exclude
    no_implementation no_namespace
    raw_property_prefixes rename
    rename_namespace

    • Post Points: 0
  • 04-24-2005 7:46 PM In reply to

    Adding a Record

    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.
    • Post Points: 0
  • 05-02-2005 9:48 PM In reply to

    • rassul
    • Not Ranked
    • Joined on 05-02-2005
    • New Member
    • Points 10

    C language and ADO usage

    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
    • Post Points: 0
  • 07-10-2005 9:21 AM In reply to

    • angus
    • Not Ranked
    • Joined on 07-10-2005
    • New Member
    • Points 5

    I now interested in this,hope any news about this:

    • Post Points: 0
  • 07-15-2005 9:49 AM In reply to

    • carthyc
    • Not Ranked
    • Joined on 07-15-2005
    • New Member
    • Points 25

    register variable

    hai,

    can we use register variable for storing address
    • Post Points: 0
Page 1 of 2 (16 items) 1 2 Next >