CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: question

  1. #1
    Join Date
    Jul 2001
    Posts
    430

    question

    How does MS SQL server interface to C/C++ program? (I want C/C++ program to read/write db of ms sql) thanks


  2. #2
    Join Date
    Jun 2001
    Posts
    20

    Re: question

    Are you talking about an extended stored procedure? Or reading/writing directly from/to the raw SQL database files?

    ESP are relatively easy, but raw access - you're on your own I think.

    - John


  3. #3
    Join Date
    Nov 2000
    Location
    Reston, Virginia, USA
    Posts
    466

    Re: question

    The most versitile method for database I/O is odbc. A newer, slower, and less versitle way though perhaps easier is using the new DAO microsoft yucky paradigm.

    To use odbc just install an odbc driver using your control panel "Data Source" program. Then create a datasource. Then in your program instanciate a CDBObject variable.


    CDatabase m_dbCust;




    Then open the database object using the open method.


    m_dbCust.Open( _T( "MYDATASOURCE" ), FALSE,
    FALSE, _T( "ODBC;UID=JOES" ),




    You can also add a password in their if you want to avoid that naste login dialog popping up..

    To execute SQL directly from your CDatabase object use the following command structure..


    TRY
    {
    m_dbCust.ExecuteSQL( strCmd );
    }

    CATCH(CDBException, e)
    {
    // The error code is in e->m_nRetCode
    }




    if you want to read data out of the database or you want to pase through records use a CRecordset ojject in conjuntion with your CDatabase object.
    you can create one directly from your class wizard and bind it to the table you like and automajically generate all the field varibles conforming to your table.. Then you can use edit, addnew and update methodes to insert new records.. et al...

    don't forget to close your recordsett and close your database object or you could cause a fur ball.



  4. #4
    Join Date
    Jul 2001
    Posts
    430

    Re: question

    thanks.

    how can c or c++ program know (recognize) "CDatabase"? Do i need to include a header file (.h)? or do something else?


  5. #5
    Join Date
    Nov 2000
    Location
    Reston, Virginia, USA
    Posts
    466

    Re: question


    #include <afxdb.h> // CDatabase + CRecordset




    We're talking VC++ here right? If you're talking if your talking linux or solaris or something we're going to have to go back to the drawing board.


  6. #6
    Join Date
    Jul 2001
    Posts
    430

    Re: question

    I don't know if 'extended stored procedure' can interface with c/c++. If yes, please tell me where to get something to start with. And for 'directly', how to do it? In Oracle, I use Pro*C and OCI to interface with c/c++. I don't know how to interface with c/c++ by using MSSQL. thanks


  7. #7
    Join Date
    Jun 2001
    Posts
    20

    Re: question

    Ok, I think you are trying to access a SQL Server database using your C/C++ client program? [Your original email sorta implied the other way around].

    If you ARE trying to access the SQL Server database from your C/C++ client program to perform standard SQL commands (like SELECT, UPDATE, etc) you have a variety of options.

    You can use DBLib, OLE-DB, ADO, MFC or ODBC.

    DBLib is the SQL 6.5 recommended interface to SQL Server. Since versions 7.0 and later, MS recommends you use OLE-DB. DBLib is straight-up C calls to the SQL Server.

    OLE-DB is real messy to use directly since you have a lot of COM work to do. The easist OLE-DB wrapper I've used is ADO. Look for objects like Recordset, Command, Connection in the MSDN. If you are using Visual C++, you can import the ADO library [msado15.dll] so you can access the ADO COM objects directly using the _com_ptr type. The ADO object model is pretty clean and easy to use.

    You can also use MFC's CDatabase object, but that uses ODBC, which MS doesn't recommend us developers use any more. [OLE-DB provides an interface to ODBC for legacy database drivers] To use ODBC directly there are a ton of ugly C-style API calls.

    Good luck,
    - John

    PS: If I'm mistaken and you DO want your sql server to talk to a C/C++ program, let me know.


  8. #8
    Join Date
    Jul 2001
    Posts
    430

    Re: question

    Sorry for something not clear.

    I want to write a pure c or c++ program (not VC++) in which I want to include some statements that can call MS SQL db (query) and read data from db and write to db.

    Thanks again for help.


  9. #9
    Join Date
    Nov 2000
    Location
    Reston, Virginia, USA
    Posts
    466

    Re: question

    >> I want to write a pure c or c++ program (not
    >> VC++)

    You're out of luck. If you're familiar with OCI or PRO C you know that such solutions require libraries and are platform specific same with all the idea's which we've covered here. You could write write multi platform code but that would mean #define 'ing out all the platform specific code and testing the app on both platform.. double the complexity, tripple the effort.

    Use odbc as I suggest or use DBLib. Both are Microsoft specific. ODBC is more versitle if you later decide to replace mssql with oracle which is what microsoft objects about it. The performance is better I've found although it's undecernable for most tasks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured