In C + + Database Programming the ODBC API

  In the C + + programming a database ODBC API, dynamic load data source 
  1, by modifying the registry loading data sources: 
  User Data Source: HKEY_CURRENT_USER \ SOFTWARE \ ODBC \ ODBC.INI 
  Data source: HKEY_LOCAL_MACHINE \ SOFTWARE \ ODBC \ ODBC.INI 
  The different types of data sources, the registry changes are also different, but basically to be amended in two places, one of the keys ODBC.INI the establishment of a data source and a description of the same name-button, and the establishment of the sub-keys and the allocation of the relevant data sources; another in the \ ODBC.INI \ ODBC Data Sources of the establishment of a new button to tell the driver manager ODBC data source types. 
  2, loading through ODBC API: Windows System subdirectory under the dynamic link library Odbcinst.dll provides a dynamically increase, modify, and delete data sources SQLConfigDataSource function, as the VC the default document does not contain this function, and hence the use of Odbcinst.h documents will be required before the project included in the first document, in the frame work of setting attributes page Object Link / library module Odbc32.lib increase in the edit box, while ensuring that the system directory under the system32 document Odbccp32.dll. 
  3, document data source connectivity: In addition to ODBC manager, SQLDriverConnect can also add files to the data source. 
  Second, the ODBC API programming if a successful implementation of ODBC API function, or return SQL_SUCCESS SQL_SUCCESS_WITH_INFO, SQL_SUCCESS instructions can be obtained through the records of the operation of the detailed information, application instructions SQL_SUCCESS_WITH_INFO results of the implementation of a warning message, through access to detailed records of information.    If the function call fail, the return code is SQL_ERROR. 
  General, the preparation procedures ODBC mainly to a number of steps: 
  1, handles distribution environment: the variables SQLHENV a statement, calling function SQLAllocHandle. 
  Set environmental attributes: After completion of the distribution environment by setting up the environment SQLSetEnvAttr function attributes, a registered version of ODBC. 
  Handle the release of the environment: data access tasks completed, should call ahead SQLFreeHandle release distribution environment. 
  2, the allocation connection handle: a statement SQLHDBC types of variables, function calls SQLAllocHandle handle distribution. 
  Connection Properties settings: all connected SQLSetConnectAttr attributes can be set up through the function, function call can be accessed SQLGetConnectAttr connect these attributes of the current settings. 
  3, linking data sources: the different procedures and user interface can be used to connect different function 
  SQLConnect: the data source as long as the function name, user ID and password, can be connected to the. 
  SQLDriverConnect: The function used to establish a connection string to connect the source of data, it allows users to input the necessary information linking, the use of the system before the definition of data sources. 
  SQLBrowseConnect: The function to support an iterative manner to connect to data sources, the establishment of the final link, it is based on client / server architecture, local database does not support this function. 
  4, and preparations for the implementation of SQL statements 
  A distribution statement handle: Statements by calling SQLAllocHandle handle distribution function. 
  SQLSetStmrrAttr SQLGetStmrrAttr and function used to access and the provision of a statement handle the option, the use of End, calling SQLFreeHandle the release of the handle. 
  B, the implementation of SQL statements 
  SQLExecDirect: The function of the implementation of direct SQL statements, only the implementation of a SQL statement, the implementation of this function is the fastest way. 
  SQLPrepare and SQLExecute: the need for implementation of the SQL statement many times, could be ready to call SQLPrepare the implementation of SQL statements, with the implementation of SQLExecute prepared statement. 
  C, the use of parameters: parameters can be used to implement a SQL statement many times, different results. 
  SQLBindParameter responsible for function parameters defined variables, a section of the SQL statement in a parameter identifier ("?") bundled together and realize value of the transmission parameters. 
  5, Get Set Records 
  A, bundled out: First, we must focus on the distribution and record variables corresponding to the field, and then through the function SQLBindCol record field will be bundled together with the process variables, the long record fields, you can call functions directly receive data SQLGetData . 
  Binding fields according to their own needs can be all bonding, which can be bundled in a few fields. 
  SQLBindCol by calling function variables will address given to NULL value can be the end of a record field of bonding, SQLFreeStmt by calling function, which will be set SQL_UNBIND option, or directly handle the release will put an end to all tied record field set. 
  B, SQLFetch: This function is used to set the record to become the next trip, and all of the data fields have tied up the data copied to the corresponding buffer. 
  C, the cursor: application access to data through the cursor (Cursor) to achieve, in the ODBC, there are three main types of cursor: one-way cursor to scroll the cursor and block cursor. 
  Some applications do not support rolling the cursor and can block cursor, ODBC SDK provides a cursor library (ODBCCR32.DLL), the application can be set up through the link attributes (SQL_STTR_ODBC_CURSOR) activate the cursor. 
  6, records add, delete, and update: data source data can be updated in three different ways: through the use of SQLExecDirect function of the corresponding SQL statements; SQLSetPos function calls to achieve record set definition updates; call SQLBulkOperations function data updates. 
  The first approach applies to any ODBC data source, the latter two methods do not support the data source can be identified called SQLGetInfo data sources. 
  SQLBulkOperations: The function based on the current operating Collection, call before it can call SQLFetch or SQLFetchScroll access. 
  Function calls, the location of the cursor block into undefined status, therefore, should first call functions SQLFetchScroll set the cursor position. 
  7, error handling: Each ODBC API function can produce a series of operational information reflected in the diagnosis records can be used to access SQLGetDiagField function in the diagnosis of specific domain records, and can be used SQLGetDiagRec access to records of some of the common domain. 
  8, transaction processing: Panel submitted in two ways: the automatic and manual mode to mode.    Application procedures can be set by calling the function SQLSetConnectAttr attribute SQL_ATTR_AUTOCOMMIT connect automatically to the connection mode is the default attribute settings, for all the ODBC driver can adapt to this model, all statements are as a separate deal with the affairs of the . 
  Manual mode submitted to a group of SQL statements into a affairs, procedures must be explicitly call functions SQLEenTran termination of a transaction.    If the use of multiple-activated services, we must create multiple connections, each one contains a link affairs. 
  9, disconnect the data link and the release handle the environment: complete database operations, the function can be called SQLDisconnect closed with database connectivity.    Posted on 2005-04-04 09:55 HuYi's Blog reading (263) Comments (0) edit their collections quoted Category: C / C + + / VC 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply