VB.beter Database Connection Using Own Code
VB.beter Database Connection Using Own Code
CONNECTION
USING OWN CODE
THE CONNECTION
OBJECT
• The connection object is what you need to
connect to a database.
• There are different connection objects and it
depends on the type of database you are
connecting to.
• Since we are connecting to an access
database we are going to use the OLE DB
connection object.
Continued…
• OLEDB means Object Linking and
Embedding Database.
• There are different OLEDB
objects, the one we will use is
called “Jet”
• Others are SQL Server and Oracle.
HOW TO CONNECT
• Open a new project
• Add a button
• Change the name property to
btnLoad
• go to code window and type the
following codes
CONNECTION STEPS
• Dim (name of db) As String
Private Sub………
• Dim con As New OleDb.OleDbConnection
• Dim dbProvider As String
• Dim dbSource As String
• Dim MyDocumentsFolder As String
• Dim TheDatabase As String
• Dim FullDatabasePath As String
CODING CONT…
• dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
• TheDatabase = “(Name of db).accdb"
• MyDocumentsFolder =
Environment.GetFolderPath(Environment.SpecialFolder.
MyDocuments)
• FullDatabasePath = MyDocumentsFolder &(name
of db)
• dbSource =
"PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\
Users\happymusi\documents\3.2\Lodge1.accdb"
CODING CONT
• con.ConnectionString = dbProvider & dbSource
• con.Open()
• End Sub
• End Class