📄 addressbookapplication.vb
字号:
Public Class AddressBookApplication
Private DBPath As String ' Full path to the database file
Private DBFileName As String ' Database file name
Private DBDirectory As String ' Path to directory with database
Private DBConnection As String ' Database connection string
Private StartupPath As String ' Location of this program
Private ClassName As String ' Name of this class
Public Sub New()
ClassName = Me.GetType.Name
StartupPath = "/Program Files/" + System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
Dim ErrLoc As String = ClassName + ".New"
Try
Dim ConfigRow As Sys_Config.Config_Type
If ReadConfig(ConfigRow) Then
DBFileName = ConfigRow.DBFileName
DBDirectory = ConfigRow.DBDirectory
DBPath = DBDirectory & DBFileName
DBConnection = "data source=" & DBPath
Else
MessageBox.Show("Unable to read sys_config.xml")
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
End Try
End Sub
#Region " Database Initialization Functions "
Public Function DatabaseExists() As Boolean
Dim ErrLoc As String = ClassName + ".DatabaseExists" 'This is used in error messages to locate source of error
Try
If System.IO.File.Exists(DBPath) Then
Return True
Else
Return DatabaseInit()
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
Public Function DatabaseInit() As Boolean
Dim ErrLoc As String = ClassName + ".DatabaseInit" 'This is used in error messages to locate source of error
Try
Dim CloseDB As New Adr_Address(DBConnection)
CloseDB.TerminateConnection()
Try
If System.IO.File.Exists(DBPath) Then
System.IO.File.Delete(DBPath)
End If
Catch ex As Exception
REM If the file is already in use this exception will happen
End Try
REM Create Database
Dim SQLEngine As SqlServerCe.SqlCeEngine
SQLEngine = New SqlServerCe.SqlCeEngine("data source=" & DBPath)
SQLEngine.CreateDatabase()
Dim AddressDB As New Adr_Address(DBConnection)
Dim AddressRow As Adr_Address.Address_Type
AddressDB.CreateTable()
If AddressDB.ImportXML(StartupPath, AddressRow) Then
Return True
Else
MessageBox.Show("Error: Cannot import Sys_Address.xml")
Return False
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
#End Region
#Region " Adr_Address Table functions "
Public Function AddressDS() As DataSet
Dim ErrLoc As String = ClassName + ".AddressDS"
Try
Dim AddressDB As New Adr_Address(DBConnection)
Return AddressDB.GetDS
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
End Try
End Function
Public Function AddressInsert(ByVal AddressRow As Adr_Address.Address_Type) As Boolean
Dim ErrLoc As String = ClassName + ".AddressInsert"
Try
Dim AddressDB As New Adr_Address(DBConnection)
If AddressDB.InsertRecord(AddressRow) Then
Return True
Else
Return False
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
Public Function AddressDelete(ByVal Address_ID As Integer) As Boolean
Dim ErrLoc As String = ClassName + ".AddressDelete"
Try
Dim AddressDB As New Adr_Address(DBConnection)
Dim AddressRow As Adr_Address.Address_Type
AddressRow.Address_ID = Address_ID
If Not AddressDB.DeleteRecord(AddressRow.Address_ID) Then
MessageBox.Show("Error: Unable to delete address")
Return False
End If
Return True
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
Public Function AddressModify(ByVal AddressRow As Adr_Address.Address_Type) As Boolean
Dim ErrLoc As String = ClassName + ".AddressModify"
Try
Dim AddressDB As New Adr_Address(DBConnection)
If Not AddressDB.PutRecord(AddressRow, AddressRow.Address_ID) Then
MessageBox.Show("Error: Unable to update address")
Return False
Else
Return True
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
Public Function AddressGet(ByRef AddressRow As Adr_Address.Address_Type) As Boolean
Dim ErrLoc As String = ClassName + ".AddressGet"
Try
Dim AddressDB As New Adr_Address(DBConnection)
If AddressDB.GetRecord(AddressRow, AddressRow.Address_ID) Then
Return True
Else
Return False
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
#End Region
#Region " Config File Settings "
Public Function ReadConfig(ByRef ConfigRow As Sys_Config.Config_Type) As Boolean
Dim ErrLoc As String = ClassName + ".ReadConfig"
Try
Dim ConfigPath As String
ConfigPath = StartupPath + "/sys_Config.xml"
Dim ConfigDB As New Sys_Config(ConfigPath)
If ConfigDB.GetLastRecord(ConfigRow) Then
Return True
Else
MessageBox.Show("Error: Unable to read Config file")
Return False
End If
Catch ex As Exception
UnHandledError(ex.ToString(), ErrLoc)
Return False
End Try
End Function
#End Region
Public Function UnHandledError(ByVal Exception As String, ByVal Location As String) As String
MsgBox("[-->" + Location + "<--]" + Exception)
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -