#include "stdafx.h"
#include "conio.h"
#include < windows.h >
#include < sql.h >
#include < sqlext.h >
void GetConnected(unsigned char *conn)
{
SQLHANDLE henv;
SQLRETURN rc;
SQLHANDLE hconn;
SQLSMALLINT bufsize=0;
SQLINTEGER nativeerror=0;
SQLSMALLINT textlen=0;
unsigned char connStrOut[256];
unsigned char sqlstate[32];
unsigned char message[256];
rc = SQLAllocEnv(&henv);
if (rc != SQL_SUCCESS)
{
printf("\nSQLAllocEnv call failed.");
return;
}
rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hconn);
if (rc != SQL_SUCCESS)
{
SQLFreeHandle(SQL_HANDLE_ENV, henv);
printf("\nSQLAllocHandle call failed.");
return;
}
rc = SQLDriverConnect(hconn, NULL,
conn, SQL_NTS,
connStrOut, 256, &bufsize,
SQL_DRIVER_NOPROMPT);
if (bufsize!=0)
{
printf("Connected successfully.\n");
SQLDisconnect(hconn);
}
else
{
rc = SQLGetDiagRec(SQL_HANDLE_DBC, hconn, 1,
sqlstate, &nativeerror, message, 256, &textlen);
printf("SQLDriverConnect failed.\n");
if (rc!=SQL_ERROR)
printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message);
}
SQLFreeHandle(SQL_HANDLE_DBC, hconn);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
void _tmain()
{
GetConnected((unsigned char *)
"DRIVER=SQL Server;SERVER=sql.someserver.com,1433;UID=admin;PWD=sa;");
getch();
}
|