Visual Basic サンプル・アプリケーションの概要

この Visual Basic サンプル・アプリケーションは、Visual Basic を使用して DB2 Everyplace データをアクセスする方法を示します。Pocket PC (WinCE) と Win32 の 両方のオペレーティング・システムで、同じアプリケーション・ロジックとユーザー・インターフェースを持つアプリケーションを開発することができます。 DB2 Everyplace には、2 つの Visual Basic サンプル・アプリケーションが提供されています。1 つは Pocket PC (WinCE) オペレーティング・システム用であり、他の 1 つは Win32 オペレーティング・システム用 です。 これら 2 つのサンプル・アプリケーションのアプリケーション・ロジックとユーザー・インターフェースは同じです。 アプリケーション・ロジックが含まれる db2evb.bas ファイルは、両方のオペレーティング・システムで共通に使用できます。 詳細については、Visual Basic 例: 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.h、 および sqlsystm.h に含まれている各種の DB2 Everyplace 制約を定義しています。このファイルで定義しているのは、最も一般的な制約のみです。 必要であれば、sqlcli.hsqlcli1.hsqlext.h、 および sqlsystm.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, " ")
    ' ステップ 1: 環境ハンドルを割り振る。
    '
    
    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
   
    '
    ' ステップ 2: データベース・ハンドルを割り振る
    '
    
    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
                
    '
    ' ステップ 3: データベースに接続する
    '
    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
    
    '
    ' ステップ 4: ステートメント・ハンドルを割り振る。
    '
    
    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.
    '
    ' ステップ 5: 表を作成する
    '
    
    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
    '
    
    '
    ' ステップ 6: 表にデータを挿入する。
    '
    
    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
 
    '
    ' ステップ 7: 表からデータを検索する。
    '
    
    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
    
    '
    ' ステップ 8: アプリケーションの終了前に DB2e データベースとの接続をクローズする。
    '
    rc = DB2eTerminate()
    
    DB2eTest = 0
End Function

関連したタスク