📄 connectedjet.vb
字号:
' 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()
Dim iw As New InputWrapper()
Dim cmd As String
Dim buf As String
Dim index As Integer
Console.WriteLine("Enter command, quit to exit")
cmd = iw.getString("> ")
While Not cmd.Equals("quit")
Try
If cmd.Equals("show") Then
ShowList()
ElseIf cmd.Equals("add") Then
Dim id As Integer = iw.getInt("id: ")
Dim owner As String = iw.getString("owner: ")
Dim bal As Decimal = iw.getDecimal("balance: ")
AddAccount(bal, owner, id)
ElseIf cmd.Equals("remove") Then
Dim id As Integer = iw.getInt("id: ")
RemoveAccount(id)
ElseIf cmd.Equals("change") Then
Dim id As Integer = iw.getInt("id: ")
Dim owner As String = iw.getString("new owner: ")
ChangeAccount(id, owner)
ElseIf cmd.Equals("clear") Then
ClearAccounts()
ElseIf cmd.Equals("init") Then
AddAccount(100, "Bob", 1)
AddAccount(200, "Mary", 2)
AddAccount(300, "Charlie", 3)
Else
Help()
End If
Catch e As Exception
Console.WriteLine(e.Message)
If Not e.InnerException Is Nothing Then
Console.WriteLine(e.InnerException.Message)
End If
End Try
cmd = iw.getString("> ")
End While
End Sub
Private Sub Help()
Console.WriteLine("The following commands are available:")
Console.WriteLine(" show -- show accounts in database")
Console.WriteLine(" add -- add an account to database")
Console.WriteLine(" remove -- remove an account from database")
Console.WriteLine(" change -- change owner name")
Console.WriteLine(" clear -- clear accounts from database")
Console.WriteLine(" init -- initialize with starting accounts")
Console.WriteLine(" quit -- exit the program")
End Sub
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -