I can provide you a little example ... not in C++ but in C .... you can
modify it and reuse in C++ code.... the only problem is that this
example make an oracle connection... but it should be quite the same...
only the string should change
bye
Pier Paolo
Wallace ha scritto:
Hai All,
Can anyone suggest or provide me sample code for making ODBC
connection and executing simple queries from c++. I am using sql server
2000. Also please tell what are the header files to be included. I
created a DSN and now I need to make connection with that DSN and
execute query from my code.
Thanks in advance..
Please help!!!! Looking forward for the response.
#include <stdio.h>
#include "sqlmine.h"
struct data {
char nome[1024];
int nomelen;
SQL_TIMESTAMP_S TRUCT ora;
int oralen;
} data1;
void usa(void *altro)
{
printf("nome %s data creazione ",data1.nom e);
if (data1.oralen!= 0)
printf("%d-%d-%d:%d:%d:%d\n", data1.ora.year, data1.ora.month ,data1.ora.day, data1.ora.hour, data1.ora.minut e,data1.ora.sec ond);
}
SQLRETURN sqltrybind(SQLH ANDLE *sqlstmt)
{
int res;
res= SQLBindCol(sqls tmt,
1,
SQL_C_CHAR,
&(data1.nome ),
1024,
&data1.nomelen) ;
res=res+SQLBind Col(sqlstmt,
2,
SQL_C_TYPE_TIME STAMP,
&(data1.ora) ,
1024,
&data1.orale n);
return res;
}
int sqlconnect(char *server,char *user,char *pwd)
{
int res;
if (res=SQLAllocHa ndle(SQL_HANDLE _ENV,SQL_NULL_H ANDLE,&sqlenv)! =SQL_SUCCESS) printf("Errore in allocEnv\n");
if (res=res*SQLSet EnvAttr( sqlenv,SQL_ATTR _ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER )!=SQL_SUCCESS) printf("Errore in SQLATTREnv\n");
if (res=res*SQLAll ocHandle(SQL_HA NDLE_DBC,sqlenv ,&sqlconn) != SQL_SUCCESS)
{
printf("Errore in allocConn %d\n",res);
}
if (res=res*SQLCon nect(sqlconn,se rver,strlen(ser ver),user,strle n(user),pwd,str len(pwd))!=SQL_ SUCCESS) printf("Errore in Connessione\n") ;
return res;
}
/* Esegue Select */
void sqlselect(char *select, sqlbind sqlb,void *altro,usedata used)
{
int res;
SQLHANDLE sqlstmt;
res=SQLAllocHan dle(SQL_HANDLE_ STMT,sqlconn,&s qlstmt);
if (res==SQL_SUCCE SS) printf("Errore in creazione Handle Statement\n");
res=SQLPrepare( sqlstmt,select, strlen(select)) ;
if (res==SQL_SUCCE SS) printf("Errore in creazione SQLPrepare\n");
sqlb(sqlstmt);
SQLExecute(sqls tmt);
while (SQLFetch(sqlst mt)==SQL_SUCCES S)
{
used(altro);
}
SQLFreeHandle(S QL_HANDLE_STMT, sqlstmt);
}
void esegui()
{
sqlconnect("TES T","SYSTEM","MA NAGER");
sqlselect("sele ct object_name,cre ated from user_objects",s qltrybind,NULL, usa);
sqlDisconnect() ;
}
/* Disconnect */
void sqlDisconnect()
{
SQLDisconnect(s qlconn);
SQLFreeHandle(S QL_HANDLE_DBC,s qlconn);
SQLFreeHandle(S QL_HANDLE_DBC,s qlenv);
}
#include <sql.h>
#include <sqlext.h>
typedef SQLRETURN (*sqlbind)(SQLH ANDLE *sqlstmt);
typedef void (*usedata)(void *altro);
SQLHANDLE sqlenv;
SQLHANDLE sqlconn;
int sqlconnect(char *server,char *user,char *pwd);
void sqlDisconnect() ;
void sqlselect(char *select, sqlbind sqlb,void *altro,usedata used);
#include <windows.h>
#include <string.h>
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <commctrl.h>
HINSTANCE hinstp;
void startDebug();
int leggi(HWND hwnd,int obj,char *buffer,int size);
char username[256];
char datasource[256];
char pwd[256];
#include "common.h"
void startDebug()
{
int hCrt,hCrti,hCrt e;
FILE *hf,*hfi,*hfe;
AllocConsole();
hCrt = _open_osfhandle ((long)GetStdHa ndle ( STD_OUTPUT_HAND LE),_O_TEXT );
hf = fdopen( hCrt, "w" );
*stdout = *hf;
setvbuf( stdout, NULL, _IONBF, 0 );
hCrti = _open_osfhandle ((long) GetStdHandle ( STD_INPUT_HANDL E),_O_TEXT );
hfi = fdopen( hCrti, "r" );
*stdin = *hfi;
setvbuf( stdin, NULL, _IONBF, 0 );
hCrte = _open_osfhandle ((long)GetStdHa ndle ( STD_OUTPUT_HAND LE),_O_TEXT );
hfe = fdopen( hCrte, "w" );
*stderr = *hfe;
setvbuf( stderr, NULL, _IONBF, 0 );
}
int leggi(HWND hwnd,int obj,char *buffer,int size)
{
memset(buffer,0 ,size);
if (GetDlgItemText (hwnd, obj, buffer, size)) {
printf("%s\n",b uffer);
return 1;
}
return 0;
}