Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C++/CLI VC++
I want to connect a C++ win32 console application to a SQL database. I have connected a C# form application to a database but don't have any idea about connecting console application in VC++ to a SQL database. I did Google but didn't find a satisfactory answer, can anybody help with the code and query?
Posted 30-Jul-12 1:37am
Edited 30-Jul-12 5:04am
armagedescu at 30-Jul-12 7:06am
If you know how to create console application, then you have the response to half of your question.
You need to google for the same question, but omit win32 console. And yes, you have to read that you google, and understand it, before posting questions here.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Some suggestions here[^], and specific Microsoft information here[^].
JackDingler at 30-Jul-12 11:27am
Ignore the comments that say that ODBC outdated.
Microsoft announced not too long ago that OLE DB will be deprecated and ODBC will be the defacto standard.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

For example the accessing SQL Server :
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>
// Forward declarations of the error handler and message handler. 
   PDBPROCESS dbproc; // The connection with SQL Server. 
   PLOGINREC login; // The login information. 
   DBCHAR name[100];
   DBCHAR city[100];
// Install user-supplied error- and message-handling functions.
   dberrhandle (err_handler);
   dbmsghandle (msg_handler);
// Initialize DB-Library.
   dbinit ();
// Get a LOGINREC.
   login = dblogin ();
   DBSETLUSER (login, "my_login");
   DBSETLPWD (login, "my_password");
   DBSETLAPP (login, "example");
// Get a DBPROCESS structure for communication with SQL Server. 
   dbproc = dbopen (login, "my_server");
// Retrieve some columns from the authors table in the
// pubs database.
// First, put the command into the command buffer. 
   dbcmd (dbproc, "SELECT au_lname, city FROM pubs..authors");
   dbcmd (dbproc, " WHERE state = 'CA' ");
// Send the command to SQL Server and start execution. 
   dbsqlexec (dbproc);
// Process the results. 
   if (dbresults (dbproc) == SUCCEED)
// Bind column to program variables. 
      dbbind (dbproc, 1, NTBSTRINGBIND, 0, name);
      dbbind (dbproc, 2, NTBSTRINGBIND, 0, city);
// Retrieve and print the result rows. 
      while (dbnextrow (dbproc) != NO_MORE_ROWS)
         printf ("%s from %s\n", name, city);
// Close the connection to SQL Server. 
   dbexit ();
   return (0);
int err_handler (PDBPROCESS dbproc, INT severity,
INT dberr, INT oserr, LPCSTR dberrstr, LPCSTR oserrstr)
   printf ("DB-Library Error %i: %s\n", dberr, dberrstr);
   if (oserr != DBNOERR)
      printf ("Operating System Error %i: %s\n", oserr, oserrstr);
   return (INT_CANCEL);
int msg_handler (PDBPROCESS dbproc, DBINT msgno, INT msgstate,
INT severity, LPCSTR msgtext, LPCSTR server,
LPCSTR procedure, DBUSMALLINT line)
   printf ("SQL Server Message %ld: %s\n", msgno, msgtext);
   return (0);
You can use Data Sources Open Database Connectivity (ODBC) to access data from a variety of database management systems. For example, if you have a program that accesses data in a SQL database, Data Sources (ODBC) will let you use the same program to access data in a Visual FoxPro database. To do this, you must add software components called drivers to your system. Data Sources (ODBC) helps you add and configure these drivers. start->Settings->Control Panel->Administrative Tools->Data Sources (ODBC) Please see here:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,478
1 OriginalGriff 6,516
2 Peter Leow 3,567
3 Zoltán Zörgő 3,351
4 Richard MacCutchan 2,397

Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 30 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100