C language database programming-ODBC

Source: Internet
Author: User
ArticleDirectory
    • C language database programming
C Language Database Programming Reprinted from: http://www.cnblogs.com/nliao/archive/2010/09/09/1822660.html Recently, my mentor asked me to read access database data in C language. I looked around for information and finally found a way to implement it, that is, ODBC API programming. In the past, I used Java and JDBC for databases. If I didn't use ADO encapsulated by others, it was very convenient to use and programming was very simple. The C language can be used as a database, but I have never thought about it before, and even doubt whether it can write a database. In the process of searching for information, I consulted a few C language experts. Their consistent answer is that C language is used as a database, and it seems impossible. I don't have to worry about it. But my mentor told me that it can be done, and the C language can be used as a database, which can be achieved very well. Therefore, it took nearly a day to find the path. Actually, it is quite simple to understand the inside story. Now I will share the relevant knowledge with you to show you how to learn and learn for new users.

Connect to the database in C language and directly call the odbc api functions. We usually use ODBC, ADO, and so on, which are encapsulated. We cannot see general programming details. Especially in IDE, we can directly use the drag-and-drop method to achieve database connection and operations. In C language programming, everything should be done by yourself.

Specifically, common database operations are completed by the following ODBC functions: sqlallochandle, sqlconnect, sqlexecute, sqlbindcol, sqlgetdata, sqlfetch, sqldisconnect, and sqlendtran. An example of a simple database connection is as follows:

// Allocate an environment handle
Sqlallochandle (SQL _handle_env, SQL _null_handle, & envhandle );
If (envhandle! = 0)
Sqlsetenvattr (envhandle, SQL _attr_odbc_version, (sqlpointer) SQL _ov_odbc3,
SQL _is_uinteger );

If (envhandle! = 0)
Sqlallochandle (SQL _handle_dbc, envhandle, & conhandle );
// Connect to the appropriate data source
If (conhandle! = 0)
{
Retcode = sqlconnect (conhandle, (sqlchar *) "DSN-name", SQL _cnt,
(Sqlchar *) "username", SQL _cnt,
(Sqlchar *) "password", SQL _cnt );

If (conhandle! = 0 & retcode = SQL _success)
Sqlallochandle (SQL _handle_stmt, conhandle, & stmthandle );

Retcode = sqlexecdirect (effecthandle, "select * from table-name where ID <20", SQL _cnt );

While (retcode! = SQL _no_data)
{
Retcode = sqlfetch (effecthandle );
If (retcode! = SQL _no_data ){
Sqlgetdata (incluthandle, 1, SQL _c_ulong, & ID, sizeof (ID), null );

}

}

Retcode = sqlendtran (SQL _handle_dbc, conhandle, SQL _commit );
 
/**
* Termination
**/

// Free the SQL statement handle
If (then thandle! = 0)
Sqlfreehandle (SQL _handle_stmt, stmthandle );
// Terminate the data source connection
If (conhandle! = 0)
Retcode = sqldisconnect (conhandle );
// Free the connection handle
If (conhandle! = 0)
Sqlfreehandle (SQL _handle_dbc, conhandle );
// Free the environment handle
If (envhandle! = 0)
Sqlfreehandle (SQL _handle_env, envhandle );

The most authoritative odbc api programming book is Microsoft'sProgramManual, which can be downloaded in Chinese online.

Through Reading ODBC data and programming practices, I have a profound understanding that the C language is so powerful that it can be said that it is omnipotent. It's not easy to become a C-language master, but it takes a bit of effort. I remember my mentor told me that to become a computer master, I should first turn to C language.

It seems I have to work hard!

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.