Use ODBC to operate access databases in VC

Source: Internet
Author: User
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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.