1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// Handles ODBC
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
char szData[100];
short l;
//Vous devez adapter ce qui suit à votre DSN spécifique
PTSTR szDataSource = "****";
PTSTR szUID = "*****";
PTSTR szPWD = "*******";
SQLTCHAR* szSQLSelect = (SQLTCHAR*) "execute getArticle '12267/1'";
// Initialise l'environnement ODBC.
if (SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv) == SQL_ERROR)
{
cout<<"Erreur Initialisation de l'environnement ODBC"<<endl;
goto EXIT;
}
SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*) SQL_OV_ODBC3, SQL_IS_INTEGER);
// Alloue un handle de connexion et se connecte à la source de données.
if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc) == SQL_ERROR)
{
cout<<"Erreur Allouage Handle de Connexion"<<endl;
goto EXIT;
}
if (SQLConnect(hdbc, (SQLTCHAR*) szDataSource, SQL_NTS, (SQLTCHAR*) szUID,
SQL_NTS, (SQLTCHAR*) szPWD, SQL_NTS) == SQL_ERROR)
{
cout<<"Erreur Connexion"<<endl;
goto EXIT;
}
// Récupère un handle d'instruction et exécute une instruction Transact-SQL SELECT.
if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) == SQL_ERROR)
{
cout<<"Erreur Allouage Handle d'Instruction"<<endl;
goto EXIT;
}
if(SQLPrepare(hstmt, szSQLSelect, SQL_NTS) == SQL_ERROR)
{
cout<<"Erreur Préparation du Statement"<<endl;
goto EXIT;
}
if (SQLExecute(hstmt) == SQL_ERROR)
{
cout<<"Erreur Execution du Statement"<<endl;
goto EXIT;
}
if (SQLColAttribute(hstmt, NULL, SQL_DESC_COUNT, NULL, 1, NULL, buf3) == SQL_ERROR)
{
cout<<"Erreur dans SQLColAttribute"<<endl;
goto EXIT;
}
if (SQLFetchScroll(hstmt, SQL_FETCH_NEXT, NULL)== SQL_ERROR)
{
cout<<"Prochain Enregistrement Innexistant"<<endl;
goto EXIT;
}
EXIT:
if (hstmt != NULL)
{
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
}
if (hdbc != NULL)
{
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
if (henv != NULL)
{
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
return (0);
} |
Partager