Seeking Simple Tutorial for Connecting to SQL via C++ (VS2005)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HumanJHawkins

    Seeking Simple Tutorial for Connecting to SQL via C++ (VS2005)

    Hi,

    I've done several database apps with PHP to SQL, or C++ to MS Access,
    etc. And I went to the VS2005 / SQL 2005 launch event where the
    presented built a simple DB app in seconds on stage. (Though he did it
    with .asp for a web site rather than C++)

    Now, I am trying to use a C++ / MFC framework I have to make an app
    that needs to connect to SQL. But... When I fire up Visual Studio and
    search around the MSDN library, I can't seem to figure it out.

    Ideally, can someone point to a web site (or just post some code). I
    would also love to hear a recomendation for an "in 21 days" type book
    on this.

    Thanks in advance for any help.
  • HumanJHawkins

    #2
    Re: Seeking Simple Tutorial for Connecting to SQL via C++ (VS2005)

    On Nov 11, 2:38 pm, Erland Sommarskog <esq...@sommars kog.sewrote:
    One option is used managed C++ and .Net. That would permit you to use
    SqlClient from ADO .Net, which I think is a very good API.
    >
    If you want to work with native code, you two have options ODBC and OLE DB.
    I will try ADO.net, but that is probably going to take a bit more
    learning... I have never used either .net, or managed C++ before. But
    though I forgot to say this in my original post, I am hoping to figure
    out how to use ADO (not .net) for now.

    I've included "msado25.h" , which gives access to the _Connection and
    _Recordset objects. And, I've found numerous examples for using it
    from VB on the web which I have attempted to translate into C++, such
    as:

    CString csServer = _T("TestServer" );
    CString csDatabase = _T("TestDatabas e");
    CString csSQLConnectStr ing;
    csSQLConnectStr ing.Format(_T(" Provider=MSDATA SHAPE;Data
    Provider=SQLOLE DB;")
    _T("server=%s;D atabase=%s;Inte grated Security='SSPI' ;"),
    m_csServer, m_csDatabase);
    CString csQuery = _T("SELECT * FROM Employees");

    VARIANT vSQLVariant;

    _Connection sqlConnection;
    _Recordset sqlRecordset;

    // In VB it would be:
    // sqlConnection.O pen AccessConnect
    // Set sqlRecordset = sqlConnection.E xecute("SELECT * FROM
    Employees")
    sqlConnection.O pen(csSQLConnec tString,_T(""), _T(""),0);
    sqlRecordset = sqlConnection.E xecute(csSQLCon nectString,
    &vSQLVariant , 0);

    However, this doesn't work... Obviously I'm not using the connection
    and recordset objects right, but I can't find any documentation on
    them.

    Any advice?
    Thanks in advance.

    Comment

    • Erland Sommarskog

      #3
      Re: Seeking Simple Tutorial for Connecting to SQL via C++ (VS2005)

      HumanJHawkins (JHawkins@Locut ius.Com) writes:
      I will try ADO.net, but that is probably going to take a bit more
      learning... I have never used either .net, or managed C++ before. But
      though I forgot to say this in my original post, I am hoping to figure
      out how to use ADO (not .net) for now.
      Yes, I forgot that you can use ADO from C++ as well. ADO is certainly
      more high level than OLE DB, but ADO also does a lot of things behind
      your back. Overall, I dislike ADO as an API. ADO .Net is something entirely
      different, and a lot better.

      I think you are better off with ODBC than ADO.
      // In VB it would be:
      // sqlConnection.O pen AccessConnect
      // Set sqlRecordset = sqlConnection.E xecute("SELECT * FROM
      Employees")
      sqlConnection.O pen(csSQLConnec tString,_T(""), _T(""),0);
      sqlRecordset = sqlConnection.E xecute(csSQLCon nectString,
      &vSQLVariant , 0);
      >
      However, this doesn't work... Obviously I'm not using the connection
      and recordset objects right, but I can't find any documentation on
      them.
      In the call to sqlConnection.E xecute, shouldn't you pass the query
      text, rather then the connection string.

      Also, in the connection string:
      csSQLConnectStr ing.Format(_T(" Provider=MSDATA SHAPE;Data
      Provider=SQLOLE DB;")

      While MSDATASHAPE maybe useful with ADO, start with a regular
      provider like SQLNCLI. SQLNCLI is the provider that ships with
      SQL 2005. SQLOLEDB does not give full support for all new
      features in SQL 2005.

      The same applies to ODBC: use SQL Native Client, not the SQL Server
      as the driver.


      --
      Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

      Links for SQL Server Books Online:
      SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
      SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
      SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

      Comment

      Working...