Access SQL database directly through the ODBC API

zhaozj2021-02-17  49

****************************************

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!"

End

END 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

End

END IF

__________________________________________________

3. Connect Using The Sconnect String - SqldriverConnect

DIM SRESULT AS STRING

DIM isize as integer

DIM 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 Long

DIM ILEN AS INTEGER

DIM SSQLSTATE AS STRING * MAX_DATA_BUFFER

DIM SERRORMSG AS STRING * MAX_DATA_BUFFER

DIM 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 INTEGER

DIM SDATA AS STRING * MAX_DATA_BUFFER

Dim Loutlen As Long

Bperform = SQLFETCH (GLSTMT)

Do While Bperform

Bperform = SQLFETCH (LSTMT) 'Get The Next Row of Data

IF BPERFORM = SQL_SUCCESS TEN 'IF ROWS OF Data Available

Bperform = true 'get author name - icolumn = 1 for first field i.e. 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, LOUTLEN)

'Add the Field Data To Correponding Data Display Controls for this Row

Else

Bperform = false 'no more rows available

END IF

Loop

'Release The Odbc Statement Handle

Bperform = SQLFREESTMT (GLSTMT, SQL_DROP)

__________________________________________________

8. Release The Odbc Statement Handle - SQLFreeStmt

Code in Step 7.

__________________________________________________

*********************************************************** ******************

The Steps 9 - 11 Are 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)

__________________________________________________

*********************************************************** ********************

The Following Entries Are Required in The Odbcapi Module

*********************************************************** ********************* 'ODBC VARIABLES AND CONSTANTS

Global Glenv As Long

Global GLDBC As Long

Global SSQL AS String

Global const Max_data_buffer = 255

Global const SQL_SUCCESS = 0

Global const SQL_SUCCESS_WITH_INFO = 1

Global const SQL_ERROR = -1

Global const SQL_NO_DATA_FOND = 100

Global const SQL_CLOSE = 0

Global const SQL_DROP = 1

Global const SQL_CHAR = 1

Global const SQL_NUMERIC = 2

Global const SQL_DECIMAL = 3

Global const SQL_INTEGER = 4

Global const SQL_SMALLINT = 5

Global const SQL_FLOAT = 6

Global const SQL_REAL = 7

Global const SQL_DOUBLE = 8

Global const SQL_VARCHAR = 12

Global const SQL_DATA_SOURCE_NAME = 6

Global const SQL_USER_NAME = 8

'ODBC Declarations

'THE HWND IS A Long in 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 IntegerDeclare 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 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 INTEGER

Declare function sqlexecdirect lib "odbc32.dll" (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 in ONTEGER, _

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 long, _

Byval szinfo as string, byval cbinfomax as in 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 SQLAlocenv 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 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 Sqlstring As String, _

BYVAL SQLSTRLEN As Long) AS INTEGER

Declare Function SQLNumResultCols Lib "odbc.dll" (ByVal lStmt As Long, NumCols As Integer) As IntegerDeclare 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 integer, _

Byval szinfo as string, byval cbinfomax as in integer, _

CBINFOOUT AS INTEGER AS INTEGER

Declare function sqlerror lib "odbc.dll" (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-29558.html

New Post(0)