|
|
|
|
|
|
|
|
|
|
|
|
|
Private Sub QueryButtonClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles QueryButton.Click
'code for the query and listing the results in ResultBox goes here
End Sub
Private Sub CloseButtonClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles CloseButton.Click
Dispose()
End Sub
· Now, add the Main() using the same approach as used in VBGUI.htm. Include the database connect and disconnect calls as demonstrated in VBDB.htm (make sure you have a database to connect; the example below assumes you have a foo.mdb database):
Option Strict on
Option Explicit on
Public
Class DBMain
Public Shared MyForm As QueryForm
Public Shared Connection As System.Data.OleDb.OleDbConnection
Public Shared Sub Main()
Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=p:\courses\371\notes\foo.mdb"
Connection = New System.Data.OleDb.OleDbConnection(ConnectString)
Try
Connection.Open()
Catch e As Exception
MsgBox("Problem opening database..." + e.Message)
System.Environment.Exit(1)
End Try
Myform = New QueryForm()
Myform.ShowDialog()
Try
Connection.Close()
Catch e As Exception
MsgBox("Problem closing database..." + e.Message)
System.Environment.Exit(1)
End Try
'if all's well, exit with status 0
System.Environment.Exit(0)
End Sub
End Class
· Test the application. It should first connect to the database and then pop up the QueryForm. Clicking the Query button does not do anything yet; clicking the Close button takes the QueryForm away after which Main() closes the database connection.
· Now add the database query functionality to the QueryButtonClick() event handler (check the SQL query in the code below and make sure that either your database supports it or change the query such that your database suppots it):
Private Sub QueryButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QueryButton.Click
Dim SQLCommand As System.Data.OleDb.OleDbCommand
Dim SQLDataReader As System.Data.OleDb.OleDbDataReader
Dim result_string As String
SQLCommand = New System.Data.OleDb.OleDbCommand("select name from foo", DBMain.Connection)
Try
SQLDataReader = SQLCommand.ExecuteReader()
Catch f As Exception
MsgBox("Problem in ExecuteReader..." + f.Message)
DBMain.Connection.Close()
System.Environment.Exit(1)
End Try
Try
Do While SQLDataReader.Read()
result_string = SQLDataReader.GetString(0)
ResultList.Items.Add(result_string)
Loop
SQLDataReader.Close()
Catch g As Exception
MsgBox("Problems reading data..." + g.Message)
DBMain.Connection.Close()
System.Environment.Exit(1)
End Try
End Sub
· To top this off, add a Clear button to the QueryForm. Then write an event handler for that button clearing the result list.
Private Sub ClearButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
ResultList.Items.Clear()
End Sub
· Notice how the interface now clears out the result of the previous query when the Clear button is clicked and how new queries get issued when the Query button receives a click event.
Private Sub QueryButtonClick(ByVal sender As System.Object, __
ByVal e As System.EventArgs) Handles QueryButton.Click
Dim dt As DataTable
Dim myAdapter As OleDb.OleDbDataAdapter
Dim QueryString As String = "select sensor_name, location_name " + "from sensor, location " + "where sensor.location_id = location.location_id"
Try
myAdapter = New OleDb.OleDbDataAdapter(QueryString, DBMain.Connection)
Catch g As Exception
MsgBox("Problem creating dataAdapter..." + g.Message)
DBMain.Connection.Close()
System.Environment.Exit(1)
End Try
dt = New DataTable()
myAdapter.Fill(dt)
myAdapter.Dispose()
TheDataGrid.DataSource = dt
Refresh()
End Sub