In fact, there are many ways to operate the database, such as DAO, ADO, etc. However, these ways need to bring some runtime, less than a few megabytes, and more than ten megabytes. It was very simple to follow the operation of the database. After the release, the program has a dozen megabytes. In practice, the author summarizes the method of operating the database with the API. For some simple database operations, it can be implemented in this way. The biggest advantage is to save the support of the running library. Big simple installation package size.
Source programs can go to my website to download: http://www.j2soft.cn/
Cui Moven
Email: cuizm@163.com
First add a module, method: Menu -> Project -> Add module, code as follows:
Option expedition
Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv &) As IntegerDeclare Function SQLAllocConnect Lib "odbc32.dll" (ByVal henv &, phdbc &) As IntegerDeclare Function SQLAllocStmt Lib "odbc32.dll" (ByVal hdbc &, phstmt &) As IntegerDeclare Function SQLConnect Lib " odbc32.dll "(ByVal hdbc &, ByVal szDSN $, ByVal cbDSN%, ByVal szUID $, ByVal cbUID%, ByVal szAuthStr $, ByVal cbAuthStr%) As IntegerDeclare Function SQLColAttributesString Lib" odbc32.dll "Alias" SQLColAttributes "(ByVal hstmt &, ByVal icol%, ByVal fDescType%, ByVal rgbDesc As String, ByVal cbDescMax%, pcbDesc%, pfDesc &) As IntegerDeclare Function SQLDisconnect Lib "odbc32.dll" (ByVal hdbc &) As IntegerDeclare Function SQLExecDirect Lib "odbc32.dll" (ByVal hstmt &, ByVal szSqlStr $, ByVal cbSqlStr &) As IntegerDeclare Function SQLFetch Lib "odbc32.dll" (ByVal hstmt &) As IntegerDeclare Function SQLFreeConnect Lib "odbc32.dll" (ByVal hdbc &) As IntegerDeclare Function SQLFreeEnv Lib "odbc32.dll" (ByVal henv &) As IntegerDeclare Function SQLFRE eStmt Lib "odbc32.dll" (ByVal hstmt &, ByVal fOption%) As IntegerDeclare Function SQLGetData Lib "odbc32.dll" (ByVal hstmt &, ByVal icol%, ByVal fCType%, ByVal rgbValue As String, ByVal cbValueMax &, pcbValue &) As IntegerDeclare Function SQLSetData Lib "odbc32.dll" (ByVal hstmt &, ByVal icol%, ByVal fCType%, ByVal rgbValue As String, ByVal cbValueMax &, pcbValue &) As IntegerDeclare Function SQLNumResultCols Lib "odbc32.dll" (ByVal hstmt &, pccol%) As IntegerDeclare Function SQLNumResultRols LIB "ODBC32.DLL" (Byval HSTMT &
, PcRol%) As LongGlobal Const SQL_C_CHAR As Long = 1Global Const SQL_COLUMN_LABEL As Long = 18Global Const SQL_DROP As Long = 1Global Const SQL_ERROR As Long = -1Global Const SQL_NO_DATA_FOUND As Long = 100Global Const SQL_SUCCESS As Long = 0
Public RC As long 'Note: ODBC function return code public Henv as long' Note: ODBC Environment Handle Public HDBC As Long
Add a MSFlexGrid control to display the data queried from the database, the code is as follows:
Option expedition
Private sub fascist1_click () unload mend Sub
Private Sub Form_Load () RC = SQLAlocenv (HENV) IF RC <> 0 THEN MSGBOX "Unable to initialize ODBC" end end if rc = sqlallocconnect (henv, hdbc) IF rc <> 0 THEN MSGBOX "RC = SQLFreeenV (HENV) End end if DIM DSN AS STRING, UID AS STRING, PWD AS STRING DSN = "PowerSoft DEMO DB V6" UID = "DBA" PWD = "SQL" RC = SqlConnect (HDBC, DSN, LEN (DSN), UID , Len (UID), PWD, LEN (UID)) IF RC = SQL_ERROR THEN MSGBOX "Unable to establish a connection with ODBC data sources" unload me end ifend sub
Private Sub cmdQuery_Click () On Error Resume Next Dim hstmt As Long Dim SQLstmt As String Dim RSCols As Integer, RSRows As Long Dim i As Integer, j As Integer Dim ColVal As String * 1024 Dim ColValLen As Long, ColLabLen As Integer, larg As Long Grid1.Redraw = False rc = SQLAllocStmt (hdbc, hstmt) If rc <> SQL_SUCCESS Then MsgBox "Failed to get SQL statement handle" Exit Sub End If SQLstmt = "SELECT * FROM exam_xref_info" rc = SQLExecDirect (hstmt, SQLstmt, Len ( SQLstmt)) If rc <> SQL_SUCCESS Then MsgBox "SQL statement fails" Exit Sub End If rc = SQLNumResultCols (hstmt, RSCols) If RSCols> 1 Then Grid1.Cols = RSCols Grid1.Rows = 10 Grid1.Row = 0 Else Exit Sub End If For i = 1 To RSCols rc = SQLColAttributesString (hstmt, i, SQL_COLUMN_LABEL, ColVal, 255, ColLabLen, larg) Grid1.Col = i Grid1.Text = Left (ColVal, ColLabLen) Next i Do Until SQLFetch (hstmt) = SQL_NO_DATA_FOUND COLVAL = String $ (1024, 0) IF Grid1.Row 1> = Grid1.Rows the Grid1.Rows = Grid1.Rows 1 end if Grid1.row = Grid1.row 1 for i = 1 to RSCOLS RC = SQLGETDATA (HSTMT, I, SQL_C_CHAR, Colval, Len (Colval), Colvallen Grid1.col = I Grid1.text = Left $ (ColVal, ColValLen) Next i Loop rc = SQLFreeStmt (hstmt, SQL_DROP) Grid1.Redraw = TrueEnd SubPrivate Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer) Dim rc As Integer If hdbc <> 0 Then rc = SQLDisconnect ( HDBC) End if rc = SQLFreeConnect (HDBC) IF Henv <> 0 THEN RC = SQLFREEENV (HENV) End IFEND SUB