Skip to content

Instantly share code, notes, and snippets.

@sergioceron
Created August 12, 2012 18:10
Show Gist options
  • Save sergioceron/3333515 to your computer and use it in GitHub Desktop.
Save sergioceron/3333515 to your computer and use it in GitHub Desktop.
Connect to Windows DSN from C++
#include <windows.h>
#include <sqlext.h>
#include <sql.h>
...
HENV hEnv = NULL; // for allocating memory usingSQLAllocEnv
HDBC hDBC = NULL; // connection handler
HSTMT hStmt = NULL; // statement handler
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "myDSN"; // DataSourceName (config in windows control panel)
UCHAR szUID[]="a"; //username of the database
UCHAR szPasswd[]="a"; //password of the database
RETCODE retcode;
int rcod=1;
int i, j,no=2;
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
SQLAllocConnect (hEnv, &hDBC);
// connecting to the database
retcode = SQLConnect(hDBC,szDSN,SQL_NTS, (UCHAR *)szUID,SQL_NTS, (UCHAR *)szPasswd,SQL_NTS);
// ”retcode” will hold ‘1’ if connection is established else ’-1’
// should sleep here
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
// connected!!
}
else
{
rcod=0;
printf("Cannot connect to database\n");
}
SQLFreeConnect (hDBC);
SQLFreeEnv (hEnv);
return rcod;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment