样本 Visual Basic 应用程序的概述

Visual Basic 样本应用程序显示如何使用 Visual Basic 来存取 DB2 Everyplace 数据。您既可以在 Pocket PC(WinCE)操作系统上也可以在 Win32 操作系统上开发具有相同应用程序逻辑和用户界面的应用程序。DB2 Everyplace 附带提供了两个 Visual Basic 样本应用程序。一个用于 Pocket PC(WinCE)操作系统,另一个用于 Win32 操作系统。这两个样本应用程序的应用程序逻辑和用户界面是相同的。在这两个操作系统之间,db2evb.bas 文件(它包含应用程序逻辑)是公共的。有关更多详细信息,请参阅***

样本应用程序中包括的文件

Visual Basic 项目目录(它包含样本应用程序)位于 DB2 Everyplace 安装目录之下。对于 Pocket PC(WinCE),可以在 \db2everyplace\clients\wince\database\visualbasic 中找到这些文件。对于 Win32 操作系统,可以在 \db2everyplace\clients\win32\database\visualbasic 中找到这些文件。

Visual Basic 样本应用程序包括下列文件:

db2evb.bas
db2evb.bas 文件包含 Visual Basic 样本应用程序。可以使用该样本应用程序来帮助您编写您自己的 Visual Basic 应用程序。

db2ecli.bas
db2ecli.bas 文件是用于连接至 DB2 Everyplace 数据库的 Visual Basic 接口。

它还定义 sqlcli.hsqlcli1.hsqlext.hsqlsystm.h 中的各种 DB2 Everyplace 约束。此文件只包含最常用的约束。如果有需要的话,还可以添加 sqlcli.hsqlcli1.hsqlext.hsqlsystm.h 中的其它约束。

DB2eForms(扩展名随操作系统的不同而有所变化)
应用程序用户界面文件。

DB2eSample.exe(对于 WinCE,为 DB2eSample.vb)
应用程序可执行文件。

DB2eSample.vbw
应用程序项目文件。

DB2eSample.vbp(对于 WinCE,为 DB2eSample.ebp)
应用程序项目文件。

Visual Basic 示例:db2evb.bas

样本应用程序(db2evb.bas)中使用的主要步骤包括:

连接至 DB2 Everyplace 数据库。

存取 DB2 Everyplace 数据。

终止应用程序。

注意:
确保应用程序在退出之前关闭与 DB2 Everyplace 数据库的连接。

已向此示例添加注释来阐明样本应用程序步骤。

Option Explicit
 
Public henv    As Long          ' Environment handle
Public hdbc    As Long          ' Database handle
Public hstmt   As Long          ' Statement handle
Public rc      As Integer       ' Return code
 
Public dbpath As String        ' filesystem path where DB2e will create tables.
Public userid As String        ' Userid: not used by DB2 Everyplace.
Public pass   As String        ' Password: not used by DB2 Everyplace
 
'--------------------------------------------------------------------------------
' Function: DB2eTest
'
' Description: Function illustrating how calls to DB2 Everyplace can be made.
'
'--------------------------------------------------------------------------------
 
Public Function DB2eTest() As Integer
    Dim errmsg As String
    Dim numCols As Integer
    Dim i As Integer
    Dim retLen As Long
    Dim data As String
    Dim crtStmt As String
    Dim insStmt1 As String
    Dim insStmt2 As String
    Dim selStmt As String
    On Error Resume Next 'Important: don't ask me why, but this line is needed
                          'in every function that calls functions from db2e.dll
                          'otherwise visual basic does strange mysterious things.  
    '
    dbpath = ""
    userid = ""
    pass = ""
    '
    crtStmt = "CREATE TABLE x(a INT, b TIMESTAMP)"
    insStmt1 = "INSERT INTO x VALUES(1, CURRENT TIMESTAMP)"
    insStmt2 = "INSERT INTO x VALUES(2, CURRENT TIMESTAMP)"
    selStmt = "SELECT * FROM x"
    '
    data = String(80, " ")
    ' Step 1: allocate an environment handle.
    '
    
    DB2eForm.DB2eText.Text = vbCrLf & vbCrLf & " Allocating an environment handle" 
  
    rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, henv)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
   
    '
    ' Step 2: allocate database handle
    '
    
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & 
                             " Allocating a database handle"
    
    rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, hdbc)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
                
    '
    ' Step 3: connect to the database
    '
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & 
                             "  Connecting to the database"
    
    rc = SQLConnect(hdbc, dbpath, SQL_NTS, userid, SQL_NTS, pass, SQL_NTS)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
    
    '
    ' Step 4: allocate a statment handle.
    '
    
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & 
                             "  Allocating a statement handle"
    
    rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, hstmt)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
 
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf
    
    '
    ' Now we can use CLI function calls to execute SQL statements.
    '
    ' Step 5: Create a table
    '
    
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & "  " & crtStmt
    rc = SQLExecDirect(hstmt, crtStmt, SQL_NTS)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
    
    '
    ' Create the same table again to force an error message and
    ' see if DB2eError works.
    '
    'rc = SQLExecDirect(hstmt, "create table p(a int)", SQL_NTS)
    'If (rc <> 0) Then
    '   testmsg = MsgBox("BLA1", 1, "DB2 Everyplace Visual Basic")
    '   rc = DB2eError()
    '   testmsg = MsgBox("BLA2", 1, "DB2 Everyplace Visual Basic")
    '   rc = DB2eTerminate()
    '   testmsg = MsgBox("BLA3", 1, "DB2 Everyplace Visual Basic")
    '   Exit Function
    'End If
    '
    
    '
    ' Step 6: Insert data into the table.
    '
    
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & "  " & insStmt1
    rc = SQLExecDirect(hstmt, insStmt1, SQL_NTS)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
 
    End If
 
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & "  " & insStmt2
    rc = SQLExecDirect(hstmt, insStmt2, SQL_NTS)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
 
    End If
 
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf
 
    '
    ' Step 7: Retrieve data from table.
    '
    
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf & "  " & selStmt 
                            & vbCrLf 
    
    rc = SQLExecDirect(hstmt, selStmt, SQL_NTS)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
    
    
    rc = SQLNumResultCols(hstmt, numCols)
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
    
    Do While (SQLFetch(hstmt) = SQL_SUCCESS)
        For i = 1 To numCols
            rc = SQLGetData(hstmt, i, SQL_C_CHAR, data, 80, retLen)
            DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & "  " & data & vbCrLf
    If (rc <> 0) Then
        rc = DB2eError()
        rc = DB2eTerminate()
        Exit Function
    End If
        Next
    data = String(80, " ")
    DB2eForm.DB2eText.Text = DB2eForm.DB2eText.Text & vbCrLf
    Loop
    
    '
    ' Step 8: Close connection to DB2e database before application terminates.
    '
        rc = DB2eTerminate()
    
    DB2eTest = 0
End Function

相关任务