Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 30-Jul-12 4:04am
v2
Comments
armagedescu 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.

Some suggestions here[^], and specific Microsoft information here[^].
 
Share this answer
 
v2
Comments
JackDingler 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.

http://blogs.msdn.com/b/sqlnativeclient/archive/2011/08/29/microsoft-is-aligning-with-odbc-for-native-relational-data-access.aspx
For example the accessing SQL Server :
C++
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>
 
// Forward declarations of the error handler and message handler. 
 
int err_handler(PDBPROCESS, INT, INT, INT, LPCSTR, LPCSTR);
int msg_handler(PDBPROCESS, DBINT, INT, INT, LPCSTR, LPCSTR,
LPCSTR, DBUSMALLINT);
main()
{
   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);
}


P.S.
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:
http://msdn.microsoft.com/en-us/library/2x0tte0f.aspx
 
Share this answer
 
v2
 
Share this answer
 
Comments
Richard MacCutchan 30-Apr-15 6:30am    
This question is nearly 3 years old. Your post is too late.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900