Created
August 12, 2012 18:10
-
-
Save sergioceron/3333515 to your computer and use it in GitHub Desktop.
Connect to Windows DSN from C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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