C language SDK programming-database programming-Use of ODBC Data Source APIs

Source: Internet
Author: User

I. programming interfaces for databases in Windows:
ODBC API

Dao

Oledb

ADO

Ado. net


ODBC is a function interface.
Dao, oledb, and ADO are COM components.
Ado.net is a programming interface on the. NET platform.
MFC provides ODBC and DAO Database Class cdatabase and cdaodatabase
ATL provides a template class for oledb

 

 

Ii. Use of ODBC APIs in SDK Programming

The following is an example:

# Include <windows. h>

# Include <SQL. h>
# Include <sqlext. h>
Void main ()

{

Henv; // environment handle

Hdbc; // data source handle

Hstmt; // execution statement handle

Unsigned char datasource [] = "Data Source Name"; // The Source Name set in the ODBC source

Unsigned char user [] = "User Name ";
// Account name of the database


Unsigned char PWD [] = "password"; // Database Password


Unsigned char search [] = "select XM from Stu where XH = 0 ";
Sqlreturn retcode; // record the return status of each SQL function

// Allocate the environment handle


Retcode =
Sqlallocenv (& henv); // among others
Sqlallochandle (SQL _handle_env, SQL _null_handle, & henv );

// Set ODBC Environment version 3.0


Retcode =
Sqlsetenvattr (henv, SQL _attr_odbc_version, (void *) SQL _ov_odbc3, 0 );

// Allocate a connection handle


Retcode =
Sqlallocconnect (henv, & hdbc );
// Wait

Sqlallochandle (SQL _handle_dbc, henv, & hdbc );

// Set the connection attribute. The logon timeout value is * rgbvalue second (not available)
// Sqlsetconnectattr (hdbc, SQL _login_timeout, (sqlpointer) (rgbvalue), 0 );

 

// Directly connect to the data source

// For Windows authentication, the second and third parameters can be null or any string

// SQL _cnt is "null-terminated string"


Retcode =
Sqlconnect (hdbc, datasource, SQL _cnt, user,
SQL _nt
, PWD,
SQL _nt
);

// Allocate statement handle


Retcode =
Sqlallocstmt (hdbc, & hstmt );
// Wait for sqlallochandle (SQL _handle_stmt, hdbc, & hstmt );

// Directly execute the query statement
Retcode = sqlexecdirect (hstmt, search, SQL _cnt );

// Bind the data buffer to the corresponding fields in the database (I is the query result set column number, querydata is the binding buffer, buff_length is the buffer length)

Sqlbindcol (hstmt, I, SQL _c_char, querydata [I-1], buff_length, 0 );

// Traverse the result set to the corresponding buffer

Sqlfetch (hstmt );


/*

* Operations related to traversal results, such as display

*

*/


// Pay attention to the release order. Otherwise, an unknown error will occur!

Sqlfreehandle (SQL _handle_stmt, hstmt );
Sqldisconnect (hdbc );
Sqlfreehandle (SQL _handle_dbc, hdbc );
Sqlfreehandle (SQL _handle_env, henv );
}

 

 

Iii. Ado can be programmed directly using the SDK. You only need to import the ADO library.

 

# Import "C:/program files/common files/system/ADO/msado15.dll" no_namespace Rename ("EOF", "adoeof ")
// Import the ADO Library

# Include <windows. h>

# Include <stdio. h>

Void main ()
{
Try
{
Lpctstr databasefile = "C: // program files // Microsoft SQL Server // mssql.2 // MSSQL // data // student. MDF ";
Win32_find_data WFD;
Handle hfile = findfirstfile (databasefile, & WFD );
If (hfile = invalid_handle_value)
{
Printf ("cannot find the database file/N ");
System ("pause ");
Return;
}

Coinitialize (null );
Hresult hr;
_ Connectionptr

Myconnection;

Myconnection. createinstance (_ uuidof (connection ));
HR = myconnection-> open (_ bstr_t ("provider = Microsoft. jet. oledb.4.0; Data Source = C: // program files // Microsoft SQL Server // mssql.2 // MSSQL // data // student. MDF "),
_ Bstr_t (""),
_ Bstr_t (""),
Admodeunknown );
If (myconnection = NULL)
{
Printf ("cocould not acquire a connection interface ");
System ("pause ");
Return;
}

_ Commandptr

Pcommand;
Pcommand. createinstance (_ uuidof (command ));
Pcommand-> activeconnection = myconnection;
Pcommand-> commandtext = "select * from Stu ";

_ Recordsetptr

Precordset;
Precordset. createinstance (_ uuidof (recordset ));
Precordset-> cursorlocation = aduseclient;
Precordset-> open (idispatch *) pcommand,
Vtmissing,
Adopenstatic,
Adlockbatchoptimistic,
Adcmdunknown );

_ Variant_t column;
While (! Precordset-> adoeof)
{
Column = precordset-> getcollect ("Administrator ");
If (column. VT! = Vt_null)
Printf (char *) _ bstr_t (column ));
Printf ("/t ");
Column = precordset-> getcollect ("password ");
If (column. VT! = Vt_null)
Printf (char *) _ bstr_t (column ));
Printf ("/N ");
Precordset-> movenext ();

}

Printf ("Hello world! /N ");
Myconnection-> close ();
System ("pause ");
Couninitialize ();
}
Catch (_ com_error & E)
{
_ Bstr_t bstrerror (E. errormessage ());
Lptstr strerror = (char *) bstrerror;
Printf (strerror );
Printf ("/N ");

System ("pause ");
}

}

Related Article

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.