Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++C++/CLIVC++
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 0:37am
Edited 30-Jul-12 4:04am
v2
Comments
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
good
Please Sign up or sign in to vote.

Solution 1

Some suggestions here[^], and specific Microsoft information here[^].
  Permalink  
v2
Comments
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. http://blogs.msdn.com/b/sqlnativeclient/archive/2011/08/29/microsoft-is-aligning-with-odbc-for-native-relational-data-access.aspx
Rate this: bad
good
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. 
 
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
  Permalink  
v2

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 472
1 sanket saxena 355
2 CPallini 205
3 thatraja 200
4 OriginalGriff 195
0 Sergey Alexandrovich Kryukov 8,784
1 OriginalGriff 5,025
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


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