| |
![]() |
|
| |
![]() |
|

Private Sub QueryButton_Click(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 CloseButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)Handles CloseButton.Click
End Sub
Public Class DB
Public Shared Connection As System.Data.OleDb.OleDbConnection
Public Shared Sub DBOpen()
'Access 2003
'Dim ConnectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
'"Data Source=c:\temp\foo.accdb"
'Access 2007
Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _
"Data Source=c:\temp\foo.accdb"
Connection = New System.Data.OleDb.OleDbConnection(ConnectString)
Try
Connection.Open()
Catch e As Exception
'Pass on the exception to the caller
Throw e
End Try
End Sub
Public Shared Sub DBClose()
Try
Connection.Close()
Catch e As Exception
'Pass on the exception to the caller
Throw e
End Try
End Sub
End Class
Private Sub QueryButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)Handles QueryButton.Click
DB.DBopen()
'query goes here
DB.DBclose()
End Sub
Private Sub CloseButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)Handles CloseButton.Click
Dispose()
End Sub
Dim SQLCommand As System.Data.OleDb.OleDbCommand
Dim SQLDataReader As System.Data.OleDb.OleDbDataReader = nothing
Dim result_string As String
'Open the database connection. Exit the app if this fails
Try
DB.DBOpen()
Catch ex as Exception
MsgBox("Problem opening database..." + ex.Message)
'Since we have a problem, we exit with a nonzero status.
System.Environment.Exit(1)
End Try
'Connection is open; now do the query
SQLCommand = New System.Data.OleDb.OleDbCommand("select my_name from foo", DB.Connection) 'modify this query to fit your database
Try
SQLDataReader = SQLCommand.ExecuteReader()
Catch f As Exception
MsgBox("Problem in ExecuteReader..." + f.Message)
'Query failed; try closing the database connection on our way out
Try
DB.DBClose()
Catch g as Exception
MsgBox("Problem closing database..." + g.Message)
End Try
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)
'Query failed; try closing the database connection on our way out Try
DB.DBClose()
Catch h as Exception
MsgBox("Problem closing database..." + g.Message)
End Try
System.Environment.Exit(1)
End Try
'Query succeeded; close the database connection
Try
DB.DBClose()
Catch ex as Exception
MsgBox("Problem closing database..." + ex.Message)
'Since we have a problem, we exit with a nonzero status.
'Failed closure is not as bad as a failed opening; let's not exit and have the OS do the close
End Try
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
ResultList.Items.Clear()
End Sub
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 = nothing
Dim QueryString As String = "select my_name from foo" 'modify this query to fit your database
Try
DB.DBOpen()
Catch ex as Exception
MsgBox("Problem opening database..." + ex.Message)
System.Environment.Exit(1)
End Try
Try
myAdapter = New OleDb.OleDbDataAdapter(QueryString, DB.Connection)
Catch g As Exception
MsgBox("Problem creating dataAdapter..." + g.Message)
Try
DB.DBClose()
Catch ex as Exception
MsgBox("Problem closing database..." + ex.Message)
End Try
System.Environment.Exit(1)
End Try
dt = New DataTable()
myAdapter.Fill(dt)
myAdapter.Dispose()
DataGrid.DataSource = dt
Refresh()
End Sub