Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.OleDb
- Imports System.IO
- Public Class DB
- 'Maintain a connection
- Public Shared connection As OleDbConnection
- Public Shared reader As OleDbDataReader
- Public Shared reader2 As OleDbDataReader
- Public Shared Sub connectToDB()
- 'Connect to database
- Try
- 'Build a connection
- ' Connection String For Microsoft Office/Access 2007,2010,2013
- 'Provider=Microsoft.ACE.OLEDB.12.0
- connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" &
- "Data Source=|DataDirectory|\Database\{MyDatabaseFilename}.accdb; Jet OLEDB:Database Password={MyPassword};")
- 'Opens the connection
- connection.Open()
- Catch e As OleDb.OleDbException
- MsgBox(e.Message, Error_Code.Code_0x0001, Error_Code.Msg_0x03)
- Catch ex As Exception
- MsgBox(ex.Message)
- 'close the application
- MsgBox(ex.Message, Error_Code.Code_0x0002, Error_Code.Code_0x0002)
- End Try
- End Sub
- Public Shared Sub disconnectFromDB()
- Try
- 'Close the connection
- connection.Close()
- Catch ex As OleDb.OleDbException
- MsgBox(ex.Message, Error_Code.Msg_0x7, Error_Code.Msg_0x03)
- End Try
- End Sub
- Public Shared Sub CompactAndRepair(ByVal accessFile As String, ByVal app As Microsoft.Office.Interop.Access.Dao.DBEngine)
- Dim tempFile As String = Path.Combine(Path.GetDirectoryName(accessFile), Path.GetRandomFileName() + Path.GetExtension(accessFile))
- app.CompactDatabase(accessFile, tempFile, False)
- Dim temp As FileInfo = New FileInfo(tempFile)
- temp.CopyTo(accessFile, True)
- temp.Delete()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement