ODBC API Common Function Interpretation Remember that KONY has turned a post on my jar, explaining the use of ODBC data sources to connect to the database for a slower operation, and that post also proposes to improve the ODBC data source connection database for database operations The speed of two ways, one is to use the RDO components in Visiual Basic, one is to call the ODBC API function to perform related database operations, and you should promise the younger brother sister to write some Dongdong on the ODBC API function. Catch the fuck; the following is the commonly used fourteen functions of the ODBC API, first listing the external function reference declaration in PowerBuilder: Function Integer SQLAlocenv (Ref Long Phenv) library "odbc32.dll" Function Integer Sqlfreeeenv (long Henv) library odbc32.dll "function integer SQLDataSources (long henv, int fdirection, ref string szdsn, & integer cbdsnmax, ref integer pcbdsn, ref string szdescription, integer cbdescriptionmax, ref integer pcbdescription) library" odbc32.dll "function integer SQLAllocConnect (long henv, ref long hdbc) library "odbc32.dll" function integer SQLConnect (long hstmt, ref string szdsn, integer dsnlen, ref stringszuid, integer uidlen, ref string szpwd, integer pwdlen) library "odbc32.dll" function integer SQLDisconnect (long hdbc) library "ODBC32.DLL" FUNCTION INTEGER SQLALLOCSTMT (long HDBC, Ref Long HSTMT) library "odbc32.dll" f "f unction integer SQLTables (long hstmt, ref string sztablequalifier, integer tablequalifierlen, ref string szowner, integer owerlen, ref string szname, integer namelen, ref string sztype, integer typelen) library "odbc32.dll" function integer SQLColumns (long hstmt, ref string sztablequalifier, integer tablequalifierlen, ref string szowner, integer owerlen, ref string szname, integer namelen, ref string sztype, integer typelen) library "odbc32.dll" function integer SQLBindCol (long hstmt, integer colnum, integer datatype, ref string name, long Maxlen, Ref Long Actlen, library "odbc32.dll"
function integer SQLFetch (long hstmt) library "odbc32.dll" function integer SQLError (long lenv, long hdbc, long hstmt, ref string sqlstate, ref long nativeerror, ref string errormsg, integer errormsgmax, ref integer errormsglen) library "odbc32.dll "function integer SQLFreeStmt (long hstmt, integer Options) library" odbc32.dll "1, function integer SQLAllocEnv (ref long phenv) library" odbc32.dll "parameters: long type ref long phenv parameters passed by reference, save the function returns The handle of the OBDC environment. The value stored in PhenV became the unique identifier called after the OBDC API function. Return Value: When the Integer is successful, it returns 0, and the failed return value is less than 0. Function function: Get the ODBC environment handle. 2, Function Integer SqlallocStmt (Long HDBC, REF Long HSTMT) library "odbc32.dll" parameter: The handle of the long HDBC ODBC environment. Ref long hstmt saves the SQL statement handle. Return Value: Integer, when successful, return 0, failure return value is less than 0. Function function: Get the SQL statement handle. 3, function integer SQLConnect (long hstmt, ref string szdsn, integer dsnlen, ref string szuid, integer uidlen, ref string szpwd, integer pwdlen) library "odbc32.dll" parameter: long hstmt, call SQLAllocStmt () function to get the handle. REF STRING SZDSN, ODBC Data Source Name. Integer DSNLEN, the length of the ODBC data source name. Ref string szuid, user account. Integer uidlen, user account length. Ref string szpwd, user password. Ref integer pwdlen, user password length. Return value: Integer, when successful, the return value is greater than 0, the return value is less than 0. Function function: Connect the ODBC data source and return to the connection handle. 4, Function Integer Sqldisconnect (long HDBC) library "odbc32.dll" parameter: long hdbc connection handle. Return Value: Integer, when successful, return 0, failure return value is less than 0. Function function: Disconnect the ODBC data source.
5, function integer SQLTables (long hstmt, ref string sztablequalifier, integer tablequalifierlen, ref string szowner, integer owerlen, ref string szname, integer namelen, ref string sztype, integer typelen) library "odbc32.dll" parameter: long hstmt SQL statement handle Ref string sztablequalifier table Qualifier name. The length of the Qualifier name of the Integer Tablequalifierlen table. The owner name of the Ref string Szowner table. The owner of the Integer Owerlen table. Ref string szname table name. Integer Namelen table name length. The type name of the REF STRING SZTYPE table. The type name length of the INTEger Typelen table. Return Value: Integer, when successful, return 0, failure return value is less than 0. Function function: Get information on the table. 6, function integer SQLColumns (long hstmt, ref string sztablequalifier, integer tablequalifierlen, ref string szowner, integer owerlen, ref string szname, integer namelen, ref string sztype, integer typelen) library "odbc32.dll" parameter: long hstmt SQL statement handle Ref string sztablequalifier table Qualifier name. The length of the Qualifier name of the Integer Tablequalifierlen table. The owner name of the Ref string Szowner table. The owner of the Integer Owerlen table. Ref string szname table name. Integer Namelen table name length. The type name of the REF STRING SZTYPE table. The type name length of the INTEger Typelen table. Return Value: Integer, when successful, return 0, failure return value is less than 0. Function function: Get column information for the specified table. 7, Function integer SQLBindCol (long hstmt, integer colnum, integer datatype, ref string name, long maxlen, ref long actlen) library "odbc32.dll" parameter: long hstmt SQL statement handle integer colnum 1-5 (eligibility, with Name, type, note. Integer DataType 1-8, 12, 99. Ref string Name program string variable. Long Maxlen varies. Ref long actlen variable. Return Value: Integer, when successful, return 0, failure return value is less than 0. Function function: Binding result set.
8. Function Integer SQLFETCH (Long HSTMT) library "odbc32.dll" parameter: long hstmt sql statement handle return value: Integer, when successful, return 0, failure returns less than 0. Function function: Binding result set.
9, function integer SQLError (long lenv, long hdbc, long hstmt, ref string sqlstate, ref long nativeerror, ref string errormsg, integer errormsgmax, ref integer errormsglen) library "odbc32.dll" Parameters: long lenv ODBC connection environment handle long hdbc Handle LONG HSTMT SQL Statement Handle Ref string SQLState Used to accept strings containing SQL error identifiers Used to accept the most characters that contain SQL error identification REF STRING ERRORMSG for accepting SQL error information returns The actual number return value returned by the REF INTEGER ERRORMSGLEN function: INTEGER, when successful, return 0, failure returns less than 0. Function function: Return to call the ODBC API function error. 10, Function Integer Sqlfreeenv (long Henv) library "odbc32.dll" parameter: long LENV ODBC Environment handle return value: Integer, when successful, return 0, failure return value is less than 0. Function function: Release the ODBC environment handle. 11. Function Integer SQLFReestmt (long hstmt, integer options) library "odbc32.dll" parameter: long hstmt sql statement handle. Integer Options Related options. Return Value: Integer, when successful, return 0, failure return value is less than 0. Function function: Release the SQL statement handle. 12, function integer SQLDataSources (long henv, int fdirection, ref string szdsn, integer cbdsnmax, ref integer pcbdsn, ref string szdescription, integer cbdescriptionmax, ref integer pcbdescription) library "odbc32.dll" Parameters: long henv ODBC data source environment handle. INT FDIRECTION Direction Sign 1 (Next), 2 (First), 3 (Last), 4 (Previous) REF STRING SZDSN Data Source Name Integer CBDSNMAX Data Source Name of the maximum length of REF INTEGER PCBDSN data source name actual Length REF STRING SZDESCRIPTION Data Source Description Name Integer CBDescriptionMAX Data Source Description The maximum length of the REF INTEGER PCBDESCRIPTION Data Source Description The actual length return value of the string: INTEGER, when successful, return 0, failure return value less than 0. Function function: Release the SQL statement handle. 13, Function Integer SQLALLOCCONNECT (Long Henv, Ref Long HDBC) library "odbc32.dll" parameter: Long Henv ODBC Data Source Environment Handle. Ref long hdbc saves the ODBC connection handle. Return Value: Integer, when successful, return 0, failure return value is less than 0.
Function function: Get the ODBC connection handle. - :) The above finally describes the listed ODBC API functions, and the like has a time combination of specific examples to explain how to use the ODBC API function to access the database - :) routine: Define instance variables: protected: long henv // sql environment handle long hstmt // sql statement handle long hdbc // sql connection handle define handle external function references: function integer SQLAllocEnv (ref long phenv) library "odbc32.dll" function integer SQLFreeEnv (long henv) library "odbc32.dll" function integer SQLDataSources (long henv, int fdirection, ref string szdsn, & integer cbdsnmax, ref integer pcbdsn, ref string szdescription, integer cbdescriptionmax, ref integer pcbdescription) library "odbc32.dll" function integer SQLAllocConnect (long henv, ref long hdbc) library " odbc32.dll "function integer SQLConnect (long hstmt, ref string szdsn, integer dsnlen, ref stringszuid, integer uidlen, ref string szpwd, integer pwdlen) library" odbc32.dll "function integer SQLDisconnect (long hdbc) library" odbc32.dll " Function Integer SqlallocStmt (Long HDBC, Ref Long HSTMT) library "odbc32.dll" function integer sqltables (long hstmt, ref string sztablequalifier, integer Tablequalifierlen, Ref string szowner, integer owerlen, ref string szname, integer namelen, ref string sztype, integer typelen) library "odbc32.dll" function integer SQLColumns (long hstmt, ref string sztablequalifier, integer tablequalifierlen, ref string szowner, integer owerlen, ref string szname, integer namelen , ref string sztype, integer typelen) library "odbc32.dll" function integer SQLBindCol (long hstmt, integer colnum, integer datatype, ref string name, long maxlen, ref long actlen) library "odbc32.dll" function integer SQLFetch (long hstmt ) library "odbc32.dll"
function integer SQLError (long lenv, long hdbc, long hstmt, ref string sqlstate, ref long nativeerror, ref string errormsg, integer errormsgmax, ref integer errormsglen) library "odbc32.dll" function integer SQLFreeStmt (long hstmt, integer Options) library " odbc32.dll "declare a function wf_sqlerror () code is as follows: string ls_sqlstate, ls_errormsginteger li_errormsgmax, li_retlong nativeerrorls_errormsg = space (255) ls_sqlstate = space (255) li_ret = sqlerror (henv, hdbc, hstmt, ls_sqlstate, li_nativeerror, ls_errormsg, 255, li_errormsgmax) messagebox ( "ODBC:" open event ls_sqlstate, ls_errormsg) return of the window, the code is written as follows: string ls_dsn, ls_descriptioninteger li_direction, li_dsnmax, li_dsn_leninteger li_descriptionmax, li_description_len, li_retvalls_dsn = space (255) li_dsnmax = len (li_dsn) ls_description = space (255) li_descriptionmax = len (ls_description) if sqlallocenv (henv) = - 1 thenwf_sqlerror () elseli_driection = 1do while sqldatasources (henv, li_direction, ls_dsn, li_dsnmax, li_dsn_len, ls_description, li_descriptionmax, li_description_len) = 0lb_datasources.additem (ls_dsn0l Write the following oopend if close window event: sqldisconnect (hdbc) sqlfreeenv (henv) code is written as follows cliked cb_connect button's event: integer li_retstring ls_dsn, ls_uid, ls_pwdstring ls_qualifer, ls_owner, ls_name, ls_type, ls_tablelong ll_lenls_dsn = lb_datasources.selecteditem () ls_uid = sle_uid.textls_pwd = sle_pwd.textli_ret = sqlallocconnect (henv, hdbc) if li_ret <0 then wf_sqlerror () else li_ret = sqlconnect (hdbc, ls_dsn, len (ls_dsn), ls_uid, len (ls_uid) , LS_PWD, LEN (LS_PWD)) if Li_RET <0 THEN WF_SQLERROR () else li_ret = SQLAllocStmt (HSTMT) IF Li_RET <0 THEN WF_SQLERROR () else ls_type = "'table'
, 'VIEW' "if cbx_systemtables.checked then ls_type = ls_type ", 'SYSTEM TABLE' "end if li_ret = SQLTABLES (hstmt, ls_qualifier, len (ls_qualifier), ls_owner, len (ls_owner), ls_name, len (ls_name), ls_type , LEN (LS_TYPE)) IF Li_RET <0 THEN WF_SQLERROR () Else Ls_Table = Space (255) LL_LEN = 255 SQLbincol (HSTMT, 3, 1, LS_TABLE, LL_LEN, LL_EN) LB_TABLES.SETREDRAW (false) Do While Sqlfetch (HSTMT) = 0 lb_tables.additem (ls_table) loop lb_tables.setredraw (true) li_ret = sqlfreestmt (hstmt, 0) end if end ifend ifend ifthis.enabled = falsethis.default = falsecbx_systemtables.enabled = false event lb_datasources written in the following selectionchanged Code: sle_uid.text = "" sle_pwd.text = "" sqldisconnect (hdbc) cbx_systemtables.enabled = truecb_connect.enabled = truelb_columns.reset () lb_tables.reset () sle_uid.setfocus () cb_connect.default = true in the selectionchanged lb_tables The following code is written in the event: Intege r li_ret, li_lenstring ls_qualifier, ls_owner, ls_name, ls_column, ls_colname, ls_typelong ll_maxlenlb_columns.reset () ls_qualifier = space (255) ls_owner = space (255) ls_column = space (255) ls_colname = space (255) ls_type = space (255) ls_name = space (255) ls_name = this.selecteditem () li_ret = sqlcolumns (hstmt, ls_quanlifier, 0, ls_owner, 0, ls_name, len (ls_name), ls_column, 0) if li_ret <0 then wf_sqlerror () else ls_colname = space (255) LS_TYPE = Space (255) LL_Maxlen =