Access SQL database directly through the ODBC API

zhaozj2021-02-11  219

******************************** ODBC - Open Database Connectivity ********************** ********************

Basic Steps

Connecting to the SQL Server Database for Retrieving Information from tables. INFORMATION

*********************************************************** *********** THE Steps 1 - 3 Are for Connecting to the SQL Server Database **************************** *****************************************

ALLOCATE ODBC Environment Handle

If SQLALLOCENV (Glenv) <> 0 THEN MSGBOX "Unable to Initialize ODBC API DRIVERS!" Endend IF___________________________________________________________________________________________________________________________________________________________________________________________________________________________________

2. Allocate ODBC Database Handle

DIM ISTATUS AS INTEGER

IF SQLACCONNECT (Glenv, GLDBC) <> 0 THEN MSGBOX "Could Not Allocate Memory for Connection Handle!" ODBCINIT = FALSE

'Free the environment istatus = SQLFreeEnv (LENV)

If iSTATUS = SQL_ERROR THEN MSGBOX "Error Freeing Environment from Odbc Drivers" End IF

'Quit the application endend IF__________________________________________________________________________________________________________________________________

3. Connect Using The Sconnect String - SqldriverConnect

DIM SRESULT AS STRINGDIM ISIZE AS INTEGERDIM SCONNECT AS STRING

Sconnect = "DSN =" & GSDSN & "; UID =" & gsloginid & "; pwd =" & gsword "; app =" & gsappcode & "; database =" & gsdatabase

If SQLDriverConnect (glDbc, Screen.ActiveForm.hWnd, sConnect, Len (sConnect), sResult, Len (sResult), iSize, 0) <= 0 Then MsgBox "Could not establish connection to ODBC driver!" End If______________________________________________________________

*********************************************************** * The Steps 4 - 8 Are for Retrieving Data from Tables ************************************************************* ************* 4. Allocate ODBC Statement Handle

IF SQLACSTMT (GLDBC, GLSTMT) <> 0 THEN

Msgbox "Could Not Allocate Memory for A Statement Handle!"

END IF___________________________________________________

5. Execute ODBC Statement - SQLEXECDIRECT

Dim Lret As Long, LERRNO AS LONGDIM ILEN AS ILERDIM SSQLSTATATE AS STRING * MAX_DATA_BUFFERDIM SERRORMSG AS STRING * MAX_DATA_BUFFERDIM SMSG AS STRING

SSQL = "SELECT NAME, LOCATION from Authors"

If SQLExecDirect (glStmt, sSQL, Len (sSQL)) <> SQL_SUCCESS Then 'Also Check for ODBC Error message - SQLError lRet = SQLError (glEnv, gldbc, glStmt, sSQLState, lErrNo, sErrorMsg, MAX_DATA_BUFFER, iLen) sMsg = "Error Executing SQL Statement "& chr $ (13) & chr $ (10) smsg = smsg &" odbc state = "& trim $ (SSQLState, Instr (ssqlstate, chr $ (0)) - 1) & chr $ (13) & Chr $ (10) sMsg = sMsg & "ODBC Error Message =" & Left $ (sErrorMsg, iLen) MsgBox sMsg, vbInformation, "Execute Query" End If______________________________________________________________

6. Fetch One Row of Results from Executed Odbc Statement - SQLFETCH

Code in step 7 .__________________________________________________________________________________________________________________________

7. Get The Data in Each Field of The Fetched Row - Sqlgetdata

DIM BPERFORM AS INTEGER, ISTATUS AS INTEGERDIM SDATA AS STRING * MAX_DATA_BUFFERDIM LOUTLEN AS Long

Bperform = SQLFETCH (GLSTMT)

Do While bPerform bPerform = SQLFetch (lStmt) 'Get the next row of data If bPerform = SQL_SUCCESS Then' If rows of data available bPerform = True 'Get Author Name - iColumn = 1 for first field ie name in sSQL iStatus = SQLGetData (glStmt , Icolumn, 1, SDATA, MAX_DATA_BUFFER, LOUTLEN

'Loutlen = Length of the Valid Data in SDATA' DATA VALUE WILL BE = Left $ (SDATA, LOUTLEN), LOUTLEN = -1 if no data or null data

'Get location - iColumn = 2 for second Field I.E. location in ssql istatus = sqlgetdata (glstmt, icolumn, 1, sdata, max_data_buffer, listlen)

'Add the field data to correponding data display controls for this row else bperform = false' no more rows available endiffoop

'Release the odbc statement handlebperform = SQLFREESTMT (GLSTMT, SQL_DROP) ___________________________________________________________________________________

8. Release The Odbc Statement Handle - SQLFreeStmt

Code in step 7 .__________________________________________________________________________________________________________________________

*********************************************************** **************** THE Steps 9 - 11 is for disconnecting from the SQL Server Database ****************************** ******************************************************

9. Disconnect from ODBC Database - Sqldisconnect

ISTATUS = SQLDISCONNECT (GLDBC) _____________________________________________________________________________________________________________________________________________________________________________

10. Release The ODBC Database Handle - SQLFreeConnect

ISTATUS = SQLFreeConnect (GLDBC) ____________________________________________________________________________________________________________________________________________________________________________________________________

11. Release the odbc environment handle - SQLFreeenV

iStatus = SQLFreeEnv (Glenv) _________________________________________________________________________________________________________________________________________________________________

*********************************************************** ****************************************************************************************************************************************************TION *********************************************************** * 'ODBC Variables and Constants Global glEnv As LongGlobal glDbc As LongGlobal sSQL As String Global Const MAX_DATA_BUFFER = 255Global Const SQL_SUCCESS = 0Global Const SQL_SUCCESS_WITH_INFO = 1Global Const SQL_ERROR = -1Global Const SQL_NO_DATA_FOUND = 100Global Const SQL_CLOSE = 0Global Const SQL_DROP = 1Global Const SQL_CHAR = 1Global const SQL_NUMERIC = 2Global const SQL_DECIMAL = 3Global const SQL_INTEGER = 4Global const SQL_SMALLINT = 5Global const SQL_FLOAT = 6Global const SQL_REAL = 7Global const SQL_DOUBLE = 8Global const SQL_VARCHAR = 12Global const SQL_DATA_SOURCE_NAME = 6Global const SQL_USER_NAME = 8

'Odbc declarations'the hwnd is a long in window Windows 95 & Windows NT

#If Win32 Then Declare Function SQLAllocEnv Lib "odbc32.dll" (env As Long) As Integer Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal env As Long) As Integer Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal env As Long, ldbc As Long) As Integer Declare Function SQLConnect Lib "odbc32.dll" (ByVal ldbc As Long, ByVal Server As String, ByVal serverlen As Integer, ByVal uid As String, ByVal uidlen As Integer, ByVal pwd As String, ByVal pwdlen As integer) AS Integer

Declare Function SQLDriverConnect Lib "odbc32.dll" (ByVal ldbc As Long, ByVal hWnd As Long, ByVal szCSIn As String, ByVal cbCSIn As Integer, ByVal szCSOut As String, ByVal cbCSMax As Integer, cbCSOut As Integer, ByVal f As Integer) As Integer

Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal ldbc As Long) As Integer Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal ldbc As Long) As Integer Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal ldbc As Long, lStmt As Long) As Integer Declare Function SQLFreeStmt Lib "odbc32.dll" (ByVal lStmt As Long, ByVal EndOption As Integer) As Integer Declare Function SQLTables Lib "odbc32.dll" (ByVal lStmt As Long, ByVal q As Long, ByVal cbq As Integer, ByVal o As Long, ByVal cbo As Integer, ByVal t As Long, ByVal cbt As Integer, ByVal tt As Long, ByVal cbtt As Integer) As IntegerDeclare Function SQLExecDirect Lib "odbc32.dll" (ByVal lStmt As Long, ByVal sqlString AS String, Byval Sqlstrlen As Long AS Integer

Declare Function SQLNumResultCols Lib "odbc32.dll" (ByVal lStmt As Long, NumCols As Integer) As Integer Declare Function SQLDescribeCol Lib "odbc32.dll" (ByVal lStmt As Long, ByVal colnum As Integer, ByVal colname As String, ByVal Buflen As Integer , ColNamelen As INTEGER, DTYPE AS INTEGER, DL As Long, DS AS INTEGER, N AS INTEGER AS INTEGER

Declare Function SQLFetch Lib "odbc32.dll" (ByVal lStmt As Long) As Integer Declare Function SQLGetData Lib "odbc32.dll" (ByVal lStmt As Long, ByVal col As Integer, ByVal wConvType As Integer, ByVal lpbBuf As String, ByVal dwbuflen As Long, LPCBOUT AS Long AS Integer

Declare function sqlgetinfo lib "odbc32.dll" (byval ldbc as long, byval hwnd as stay, byval szinfo as string, byval cbinfomax as integer, cbinfoout as integer) AS integer

Declare Function SQLError Lib "odbc32.dll" (ByVal env As Long, ByVal ldbc As Long, ByVal lStmt As Long, ByVal SQLState As String, NativeError As Long, ByVal Buffer As String, ByVal Buflen As Integer, Outlen As Integer) As Integer #Else Declare Function SQLAllocEnv Lib "odbc.dll" (env As Long) As Integer Declare Function SQLFreeEnv Lib "odbc.dll" (ByVal env As Long) As Integer Declare Function SQLAllocConnect Lib "odbc.dll" (ByVal env As Long, ldbc As Long) As Integer Declare Function SQLConnect Lib "odbc.dll" (ByVal ldbc As Long, ByVal Server As String, ByVal serverlen As Integer, ByVal uid As String, ByVal uidlen As Integer, ByVal pwd As String, ByVal pwdlen As Integer AS Integer

Declare Function SQLDriverConnect Lib "odbc.dll" (ByVal ldbc As Long, ByVal hWnd As Integer, ByVal szCSIn As String, ByVal cbCSIn As Integer, ByVal szCSOut As String, ByVal cbCSMax As Integer, cbCSOut As Integer, ByVal f As Integer) As Integer

Declare Function SQLFreeConnect Lib "odbc.dll" (ByVal ldbc As Long) As Integer Declare Function SQLDisconnect Lib "odbc.dll" (ByVal ldbc As Long) As Integer Declare Function SQLAllocStmt Lib "odbc.dll" (ByVal ldbc As Long, lStmt As Long) As Integer Declare Function SQLFreeStmt Lib "odbc.dll" (ByVal lStmt As Long, ByVal EndOption As Integer) As Integer Declare Function SQLTables Lib "odbc.dll" (ByVal lStmt As Long, ByVal q As Long, ByVal cbq As INTEGER, BYVAL O As Long, Byval CBO AS INTEGER, BYVAL T As Long, Byval CBT AS INTEGER, BYVAL TT AS Long, BYVAL CBTT AS INTEGER AS INTEGER

Declare Function SQLExecDirect Lib "odbc.dll" (ByVal lStmt As Long, ByVal sqlString As String, ByVal sqlstrlen As Long) As IntegerDeclare Function SQLNumResultCols Lib "odbc.dll" (ByVal lStmt As Long, NumCols As Integer) As Integer Declare Function SQLDescribeCol lib "odbc.dll" (ByVal lStmt As Long, ByVal colnum As Integer, ByVal colname As String, ByVal buflen As Integer, colnamelen As Integer, dtype As Integer, dl As Long, ds As Integer, n As Integer) As Integer

Declare Function SQLFetch Lib "odbc.dll" (ByVal lStmt As Long) As Integer Declare Function SQLGetData Lib "odbc.dll" (ByVal lStmt As Long, ByVal col As Integer, ByVal wConvType As Integer, ByVal lpbBuf As String, ByVal dwbuflen As Long, LPCBOUT AS Long AS Integer

Declare function sqlgetinfo lib "odbc.dll" (byval ldbc as long, byval hwnd as string, byval cbinfomax as integer, cbinfoout as integer) AS integer

Declare Function SQLError Lib "odbc.dll" (ByVal env As Long, ByVal ldbc As Long, ByVal lStmt As Long, ByVal SQLState As String, NativeError As Long, ByVal Buffer As String, ByVal Buflen As Integer, Outlen As Integer) As Integer

#End IF

转载请注明原文地址:https://www.9cbs.com/read-4852.html

New Post(0)