Const char szconnect [] = "driver = {Microsoft Access Driver (*. mdb)}; DBQ = ";
Const char szdbname [] = "studdata. mdb ";
Void odbcconnect (hwnd );
Void odbcdisconnect (hwnd );
Void odbcquery (char *);
Void odbcqueryend (void );
Void fetchresult (void );
Void odbcconnect (hwnd hdlg)
{
Try {
Sqlreturn SR;
Sr = sqlallochandle (SQL _handle_env, SQL _null_handle, & henv );
If (SR! = SQL _success & Sr! = SQL _success_with_info) Throw SR;
Sr = sqlsetenvattr (henv, SQL _attr_odbc_version, (sqlpointer) SQL _ov_odbc3, 0 );
If (SR! = SQL _success & Sr! = SQL _success_with_info ){
Sqlfreehandle (SQL _handle_env, henv );
Throw SR;
}
Sr = sqlallochandle (SQL _handle_dbc, henv, & hconn );
If (SR! = SQL _success & Sr! = SQL _success_with_info ){
Sqlfreehandle (SQL _handle_env, henv );
Throw SR;
}
Strcpy (szconnectstring, szconnect );
Strcat (szconnectstring, szdbname );
Sr = sqldriverconnect (hconn, hdlg, (unsigned char *) szconnectstring, sizeof (szconnectstring), (unsigned char *) connbuf, sizeof (connbuf), (short *) & strlen, SQL _driver_complete );
If (SR! = SQL _success & Sr! = SQL _success_with_info ){
Sqlfreehandle (SQL _handle_dbc, hconn );
Sqlfreehandle (SQL _handle_env, henv );
Throw SR;
}
}
Catch (sqlreturn ){
MessageBox (hdlg, "database connection failed! "," Sorry ", mb_ OK | mb_iconwarning );
Exitprocess (0 );
}
}
Void odbcdisconnect (hwnd hdlg)
{
Sqldisconnect (hconn );
Sqlfreehandle (SQL _handle_dbc, hconn );
Sqlfreehandle (SQL _handle_env, henv );
}
Void odbcquery (char * query)
{
Try {
Sqlreturn SR;
Sr = sqlallochandle (SQL _handle_stmt, hconn, & hstmt );
If (SR! = SQL _success & Sr! = SQL _success_with_info) Throw SR;
Sr = sqlexecdirect (hstmt, (unsigned char *) query, strlen (query ));
If (SR! = SQL _success & Sr! = SQL _success_with_info ){
Sqlfreehandle (SQL _handle_stmt, hstmt );
Throw SR;
}
}
Catch (sqlreturn ){
Char errorbuf [128];
Sqlerror (henv, hconn, hstmt, null, null, (unsigned char *) errorbuf, sizeof (errorbuf), (short *) & strlen );
MessageBox (null, "database query failed! "," Sorry ", mb_ OK | mb_iconwarning );
MessageBox (null, errorbuf, "sorry", mb_ OK | mb_iconwarning );
}
}
Void odbcqueryend (void)
{
Sqlfreehandle (SQL _handle_stmt, hstmt );
}
Void fetchresult (void)
{
Sqlreturn SR;
Lv_item LVI;
Int I, row;
For (I = 0; I <10; ++ I) sqlbindcol (hstmt, I + 1, SQL _c_char, Buf [I], sizeof (BUF [I]), (long *) & strlen );
For (ROW = 0; ++ row ){
Sr = sqlfetch (hstmt );
If (SR! = SQL _success & Sr! = SQL _success_with_info) break;
LVI. Mask = lvif_text | lvif_param;
LVI. iItem = row;
LVI. isubitem = 0;
LVI. psztext = Buf [0];
Sendmessage (hlist, lvm_insertitem, 0, (long) & LVI );
LVI. Mask = lvif_text;
For (I = 1; I <10; ++ I ){
LVI. isubitem = I;
LVI. psztext = Buf [I];
Sendmessage (hlist, lvm_setitem, 0, (long) & LVI );
}
}
}