' ConnectedJet.vb
Imports System
Imports System.Data
Imports System.Data.OleDb
Module ConnectedJet
Private connStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\OI\Databases\SimpleBank.mdb"
Private conn As IDbConnection
Dim jetConn As New OleDbConnection()
Sub Main()
OpenJet()
CommandLoop()
End Sub
Private Sub OpenJet()
conn = jetConn
conn.ConnectionString = connStr
Console.WriteLine("Using Access DB SimpleBank.mdb")
Console.WriteLine("Database state: " & _
conn.State.ToString())
conn.Open()
Console.WriteLine("Database state: " & _
conn.State.ToString())
End Sub
Private Sub CommandLoop()
...
Private Sub ShowList()
Dim query As String = "select * from Account"
Dim command As IDbCommand = CreateCommand(query)
Dim reader As IDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine("{0} {1,-10} {2:C}", _
reader("AccountId"), reader("Owner"), _
reader("Balance"))
End While
reader.Close()
End Sub
Private Sub AddAccount(ByVal bal As Decimal, _
ByVal owner As String, ByVal id As Integer)
Dim query As String = "insert into Account values(" _
& id & ", '" & owner & "', ' ', " & bal & ")"
Dim command As IDbCommand = CreateCommand(query)
Dim numrow As Integer = command.ExecuteNonQuery()
Console.WriteLine("{0} rows updated", numrow)
End Sub
Private Sub RemoveAccount(ByVal id As Integer)
Dim query As String = _
"delete from Account where AccountId = " & id
Dim command As IDbCommand = CreateCommand(query)
Dim numrow As Integer = command.ExecuteNonQuery()
Console.WriteLine("{0} rows updated", numrow)
End Sub
Private Sub ChangeAccount(ByVal id As Integer, _
ByVal owner As String)
Dim query As String = _
"update Account set Owner = '" _
& owner & "' where AccountId = " & id
Dim command As IDbCommand = CreateCommand(query)
Dim numrow As Integer = command.ExecuteNonQuery()
Console.WriteLine("{0} rows updated", numrow)
End Sub
Private Sub ClearAccounts()
Dim query As String = "delete from Account"
Dim command As IDbCommand = CreateCommand(query)
Dim numrow As Integer = command.ExecuteNonQuery()
Console.WriteLine("{0} rows updated", numrow)
End Sub
Private Function CreateCommand(ByVal query As String) _
As IDbCommand
Return New OleDbCommand(query, jetConn)
End Function
End Module
|