ADO API Problem

This is a discussion on ADO API Problem within the C++ Programming forums, part of the General Programming Boards category; Can someone help me with the below ADO API? I am a new programmer and am having problems programming a ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    ADO API Problem

    Can someone help me with the below ADO API? I am a new programmer and am having problems programming a SQL Server connection. I have found various connection strings and continue to run into problems with the syntax. Thank you in advance.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    #import "C:\Program files\Common Files\System\Ado\msado15.dll" rename("EOF", "ADOEOF")
    //---------------------------------------------------------------------------------------------------------------------------------------------
    std::string outputashex(unsigned long l)
    {
    char buffer[1024];
    ::itoa(l, buffer, 16);
    return buffer;
    } ;
    //---------------------------------------------------------------------------------------------------------------------------------------------
    void main()
    {
    HRESULT hr;
    CoInitialize(NULL);
    try
    {
    	ADODB::_ConnectionPtr connection;
    	hr = connection.CreateInstance(__uuidof(ADODB::Connection));
    	if (FAILED(hr))
    {
    throw _com_error(hr);
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------
    ADODB::_RecordsetPtr recordset;
    hr = recordset.CreateInstance(__uuidof(ADODB::Recordset));
    if (FAILED(hr))
    {
    throw _com_error(hr);
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------
    connection->CursorLocation = ADODB::adUseClient;
    //--------------------------------------------------------------------------------------------------------------------------------------------
    connection->Open("Provider=sqloledb;Server=.\SQLExpress;AttachDbFilename=c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WattsALoan.mdf;Database=WattsALoan;Trusted_Connection=Yes;")
    
    
    //--------------------------------------------------------------------------------------------------------------------------------------------
    L"", ADODB::adConnectUnspecified);
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //recordset->Open("CREATE TABLE mytable (value NVARCHAR(255))",
    //connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
    //ADODB::adLockReadOnly, ADODB::adCmdText);
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //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);
    //--------------------------------------------------------------------------------------------------------------------------------------------
    recordset->Open("SELECT * from Customers",
    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();
    };
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //recordset->Close();
    //recordset->Open("DROP TABLE mytable", connection.GetInterfacePtr(),
    //ADODB::adOpenForwardOnly, ADODB::adLockReadOnly,
    //ADODB::adCmdText);
    }
    catch(_com_error &e)
    {
    std::cerr << ::outputashex(hr) << ":"
    << static_cast<char *>(e.Description());
    }
    catch(...)
    {
    std::cerr << "Unhandled Exception";
    };
    
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by forexsurfr View Post
    I have [...] run into problems with the syntax. Thank you in advance.
    what is the problem? i certainly wont read through all your code to find your problem and then solve your problem (i doubt many others will, either). it would also help to indent your code properly (i quickly looked at your attachment to see if it's formatting was correct, but it isn't).

    we are here and willing to help, but you have to tell us what your problem is! are you getting a runtime error? compiler error/warning? specifically, which line(s)/areas are you having a problem with? give as much useful information to describe your problem in order to get the most useful feedback and help.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Build Errors.

    Sorry for not including this.


    ------ Build started: Project: Trade OptimizerV15, Configuration: Debug Win32 ------
    Compiling...
    stdafx.cpp
    Compiling...
    Trade OptimizerV15.cpp
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(15) : warning C4996: 'itoa': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _itoa. See online help for details.
    c:\program files\microsoft visual studio 9.0\vc\include\stdlib.h(862) : see declaration of 'itoa'
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'S' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'P' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'M' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'M' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'M' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'D' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : warning C4129: 'W' : unrecognized character escape sequence
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : error C2660: 'ADODB::Connection15::Open' : function does not take 1 arguments
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(70) : error C2146: syntax error : missing ';' before identifier 'recordset'
    Build log was saved at "file://c:\Documents and Settings\mcertini\My Documents\Visual Studio 2008\Projects\TradeOptimizerV6\Trade OptimizerV15\Trade OptimizerV15\Debug\BuildLog.htm"
    Trade OptimizerV15 - 2 error(s), 8 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay. That means, to get a backslash in a string, you have to use \\ inside your double quotes.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    compilers are incredibly "smart"... listen to what they have to say.

    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(52) : error C2660: 'ADODB::Connection15::Open' : function does not take 1 arguments
    your passing 1 argument to this function, but it doesnt take 1 argument. if you cant figure out what to pass, tell us the functions signature.

    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(70) : error C2146: syntax error : missing ';' before identifier 'recordset'
    its very difficult to read your code, but this error is also obvious (though it may go away, or you may see new errors once you fix the previous one).

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Program With Corrections.

    Listed below is the original program almost debugged completely. It took much work to figure out the proper syntax for the connection though with help from people on this board as well as others on other boards, I was able to piece it together. I am now encountering one error. The error is listed below:

    Thank you nadroj for the help.

    ------ Build started: Project: Trade OptimizerV15, Configuration: Debug Win32 ------
    Compiling...
    Trade OptimizerV15.cpp
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(42) : error C2664: 'ADODB::Connection15::Open' : cannot convert parameter 4 from 'const char [25]' to 'long'
    There is no context in which this conversion is possible
    Build log was saved at "file://c:\Documents and Settings\mcertini\My Documents\Visual Studio 2008\Projects\TradeOptimizerV6\Trade OptimizerV15\Trade OptimizerV15\Debug\BuildLog.htm"
    Trade OptimizerV15 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Code:
    // Trade OptimizerV15.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    
    #import "C:\\Program files\\Common Files\\System\\Ado\\msado15.dll" rename("EOF", "ADOEOF")
    //---------------------------------------------------------------------------------------------------------------------------------------------
    std::string outputashex(unsigned long l)
    {
    char buffer[1024];
    _itoa_s(l, buffer, 16);
    return buffer;
    } ;
    //---------------------------------------------------------------------------------------------------------------------------------------------
    void main()
    {
    HRESULT hr;
    CoInitialize(NULL);
    try
    {
    ADODB::_ConnectionPtr connection;
    hr = connection.CreateInstance(__uuidof(ADODB::Connection));
    if (FAILED(hr))
    {
    throw _com_error(hr);
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------
    ADODB::_RecordsetPtr recordset;
    hr = recordset.CreateInstance(__uuidof(ADODB::Recordset));
    if (FAILED(hr))
    {
    throw _com_error(hr);
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------
    connection->CursorLocation = ADODB::adUseClient;
    //--------------------------------------------------------------------------------------------------------------------------------------------
    
    connection->Open("Provider=SQLOLEDB;","Data Source=PCD-LT-MCERTINI\\SQLEXPRESS;","Initial Catalog=WattsALoan.mdf;","Integrated Security=SSPI"); 
    
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //L"", ADODB::adConnectUnspecified);
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //recordset->Open("CREATE TABLE mytable (value NVARCHAR(255))",
    //connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
    //ADODB::adLockReadOnly, ADODB::adCmdText);
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //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);
    //--------------------------------------------------------------------------------------------------------------------------------------------
    recordset->Open("SELECT * from Customers",
    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();
    };
    //--------------------------------------------------------------------------------------------------------------------------------------------
    //recordset->Close();
    //recordset->Open("DROP TABLE mytable", connection.GetInterfacePtr(),
    //ADODB::adOpenForwardOnly, ADODB::adLockReadOnly,
    //ADODB::adCmdText);
    }
    catch(_com_error &e)
    {
    std::cerr << ::outputashex(hr) << ":"
    << static_cast<char *>(e.Description());
    }
    catch(...)
    {
    std::cerr << "Unhandled Exception";
    };
    
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't know how much clearer we can be than the compiler already is:
    c:\documents and settings\mcertini\my documents\visual studio 2008\projects\tradeoptimizerv6\trade optimizerv15\trade optimizerv15\trade optimizerv15.cpp(42) : error C2664: 'ADODB::Connection15::Open' : cannot convert parameter 4 from 'const char [25]' to 'long'
    Apparently the Open function doesn't take four strings, but three strings and a long.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Compiler Error.

    tapstop,

    Do you know what the error message means? I did a search on connection strings on Google and found this particular string among many others that were different in sytax for OLEDB connections. I suspect that the error maybe related to the parameter setting in the variable "connection".

    I have tried to reduce the function to three parameters and this does not work either.

    Thank you for your input.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I take it by the filename that you're using MS' ADO library? If so, the documentation would be on MSDN. I just went to MSDN, typed in the most obvious search string I could think of ("ADO C++ Open") and got this page, which conveniently lists what the arguments to Open are supposed to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout problem with windows API
    By xximranxx in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2007, 12:37 AM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21