C/Visual C - Conexion BBDD con odbc

 
Vista:

Conexion BBDD con odbc

Publicado por rraces (25 intervenciones) el 22/09/2006 14:00:52
Hola amigos, tengo el siguiente código:

#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <iostream>
#include <string>
#include <tchar.h>
void OpenDataBase(LPCTSTR szDsn, LPCTSTR szDbUid, LPCTSTR szDbPwd);

using namespace std;

int main(void)
{
LPTSTR a = _T("prueba");
LPTSTR b = _T("user");
LPTSTR c = _T("pass");

OpenDataBase(a,b,c);
return 1;
}

void OpenDataBase(LPCTSTR szDsn, LPCTSTR szDbUid, LPCTSTR szDbPwd)
{
HRESULT hRet;
HENV henv;
HDBC hdbc;
HSTMT hstmt;
LPTSTR szCnnStr;
SQLCHAR szIn[512];
SQLCHAR szOut[512];
short nOut;

//szCnnStr = _T("DSN=prueba; UID=user; PWD=pass");

//LPTSTR prueba = (_T("DSN=%s; UID=%s; PWD=%s \n"), szDsn, szDbUid, szDbPwd);


hRet = ::SQLAllocEnv(&henv);

if (hRet == SQL_SUCCESS || hRet == SQL_SUCCESS_WITH_INFO)
{
cout << "Abierto el entorno" << endl;

hRet = ::SQLAllocConnect(henv, &hdbc);
if (hRet == SQL_SUCCESS || hRet == SQL_SUCCESS_WITH_INFO)
{
cout << "Abierto la conexion bbdd" << endl;
//wsprintf(szCnnStr,_T("DSN=%s; UID=%s; PWD=%s"), szDsn, szDbUid, szDbPwd);

cout << "Convirtiendo la cadena 1111" << endl;
memcpy(szIn, szCnnStr, sizeof(szCnnStr));
cout << "Convirtiendo la cadena " << szCnnStr <<endl;

hRet = ::SQLDriverConnect(hdbc, NULL, szIn, sizeof(szIn), szOut, sizeof(szOut), &nOut, 0);
if (hRet == SQL_SUCCESS || hRet == SQL_SUCCESS_WITH_INFO)
{
cout << "Abierto la bbdd" << endl;
}
else
{
if( hRet == SQL_NO_DATA)
cout << "SQL_NO_DATA --> " << hRet << endl;
if( hRet == SQL_ERROR)
cout << "SQL_ERROR --> " << hRet << endl;
if( hRet == SQL_INVALID_HANDLE)
cout << "SQL_INVALID_HANDLE --> " << hRet << endl;
}

}


::SQLFreeConnect(hdbc);
::SQLFreeEnv(henv);

}

}

Pues mi Problema es el siguiente, si utilizo la función wsprintf me da un pantallazo de error, de estos que te dice que si lo quieres depurar, y si lo comento y pongo
szCnnStr = _T("DSN=prueba; UID=user; PWD=pass"); al principio me funciona todo correcto pero nunca me conecta a la base de datos me da SQL_ERROR.

Tengo bien configurada la DSN, me quiero conectar a una bbdd ORACLE y lo compilo con Visual C++ 6.0. Supongo que será la conversión de szCnnStr a szIn. O no se que puede ser. He probado distintas cosas pero no funciona.

Gracias y un Saludo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder