19/09/2024

AppDatos

Portal de Información – Rutificador

[C++] Conexión ODBC SQL Server en C++ Desarrollo en x64

1 minuto de lectura

Código Fuente para Conexión Entre una Aplicación de 64 Bits o X64, Desarrollada en C++ y una fuente de Datos ODBC. Llamada Directa a un Procedimiento Almacenado con Parámetros de Entrada y Salida.



Source Code for ODBC Connection, SQL Server and C++ x64.


#include 
#include 
#include 
#include 
#include 
#include    
#include   

void printErrDbc(SQLHDBC hDbc)
{
	SQLSMALLINT recNr = 1;
	SQLRETURN ret = SQL_SUCCESS;
	while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		SQLWCHAR errMsg[SQL_MAX_MESSAGE_LENGTH + 1];
		SQLWCHAR sqlState[5 + 1];
		errMsg[0] = 0;
		SQLINTEGER nativeError;
		SQLSMALLINT cb = 0;
		ret = SQLGetDiagRec(SQL_HANDLE_DBC, hDbc, recNr, (SQLWCHAR*) sqlState, &nativeError, (SQLWCHAR*) errMsg, SQL_MAX_MESSAGE_LENGTH + 1, &cb);
		if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
		{
			std::wcerr << L"ERROR; native: " << nativeError << L"; state: " << sqlState << L"; msg: " << errMsg << std::endl;
		}
		++recNr;
	}
}

void printErrStmt(SQLHSTMT hStmt)
{
	SQLSMALLINT recNr = 1;
	SQLRETURN ret = SQL_SUCCESS;
	while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		SQLWCHAR errMsg[SQL_MAX_MESSAGE_LENGTH + 1];
		SQLWCHAR sqlState[5 + 1];
		errMsg[0] = 0;
		SQLINTEGER nativeError;
		SQLSMALLINT cb = 0;
		ret = SQLGetDiagRec(SQL_HANDLE_STMT, hStmt, recNr, (SQLWCHAR*) sqlState, &nativeError, (SQLWCHAR*) errMsg, SQL_MAX_MESSAGE_LENGTH + 1, &cb);
		if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
		{
			std::wcerr << L"ERROR; native: " << nativeError << L"; state: " << sqlState << L"; msg: " << errMsg << std::endl;
		}
		++recNr;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	SQLRETURN   nResult = 0;
	SQLHANDLE   handleEnv = 0;

	nResult = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, (SQLHANDLE*)&handleEnv);

	nResult = SQLSetEnvAttr(handleEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3_80, SQL_IS_INTEGER);

	SQLHANDLE   handleDBC = 0;
	nResult = SQLAllocHandle(SQL_HANDLE_DBC, handleEnv, (SQLHANDLE*)&handleDBC);

	SQLWCHAR     strConnect[256] = L"Driver={SQL Server Native Client 11.0}; Server=localhost; Database=; Uid=; Pwd=;";
	SQLWCHAR     strConnectOut[1024] = { 0 };
	SQLSMALLINT nNumOut = 0;
	nResult = SQLDriverConnect(handleDBC, NULL, (SQLWCHAR*)strConnect, SQL_NTS, (SQLWCHAR*)strConnectOut, sizeof(strConnectOut),
		&nNumOut, SQL_DRIVER_NOPROMPT);
	if (!SQL_SUCCEEDED(nResult))
	{
		printErrDbc(handleDBC);
	}

	nResult = SQLSetConnectAttr(handleDBC, SQL_ATTR_AUTOCOMMIT, (SQLUINTEGER)SQL_AUTOCOMMIT_OFF, NULL);
	if (!SQL_SUCCEEDED(nResult))
	{
		printErrDbc(handleDBC);
	}

	SQLHSTMT    handleStatement = 0;
	nResult = SQLAllocHandle(SQL_HANDLE_STMT, handleDBC, (SQLHANDLE*)&handleStatement);
	if (!SQL_SUCCEEDED(nResult))
	{
		printErrDbc(handleDBC);
	}

	// Bind return code
	SQLINTEGER res = 0;
	SQLLEN cb = 0;
	SWORD sParm1 = 0;
	SQLLEN cbParm1 = SQL_NTS;

	nResult = SQLBindParameter(handleStatement, 1, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &sParm1, 0, &cbParm1);
	if (!SQL_SUCCEEDED(nResult))
	{
		printErrStmt(handleStatement);
	}

	// And call using synonym name
	//nResult = SQLExecDirect(handleStatement, (SQLWCHAR*)L"{CALL my_sp(param)}", SQL_NTS);
	nResult = SQLExecDirect(handleStatement, (SQLWCHAR*)L"{CALL sp_who2}", SQL_NTS);
	if (!SQL_SUCCEEDED(nResult))
	{
		printErrStmt(handleStatement);
	}

	std::cout << "SQLExecDirect returned " << nResult << "\r\n";
	if (nResult == SQL_SUCCESS_WITH_INFO || nResult == SQL_SUCCESS)
	{
		int spid;
		char status[50];
		char login[100];
		char hostname[100];
		while (SQLFetch(handleStatement) == SQL_SUCCESS)
		{
			SQLGetData(handleStatement, 1, SQL_C_ULONG, &spid, 0, NULL);
			SQLGetData(handleStatement, 2, SQL_C_CHAR, status, 50, NULL);
			SQLGetData(handleStatement, 3, SQL_C_CHAR, login, 100, NULL);
			SQLGetData(handleStatement, 4, SQL_C_CHAR, hostname, 100, NULL);
			std::cout << "spid: " << spid << " status: " << status << " login: " << login << " hostname: " << hostname << std::endl;
		}
	}

	//nResult = SQLFetch(handleStatement);
	//std::wcout << L"Result is: " << sParm1 << std::endl;

	return 0;
}

Deja una respuesta