How to Create Connection to Database with MS Access using VB.net

In This VB.Net Tutorial We Will See How to Create Connection to Database with MS Access using VB.net

Video

 Source Code:

Dim CNN As New OleDb.OleDbConnection
    Private Function OpenConnection() As Boolean
        CNN.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\db.accdb"
        CNN.Open()
        OpenConnection = True
        Exit Function
    End Function
    Private Sub CheckConnectionState()
        If Not CNN.State = ConnectionState.Open Then
            lblState.Text = "Connected"
            btnConnection.Text = "Disconnect"
            OpenConnection()
        End If
    End Sub

    Private Sub btnConnection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnection.Click
        If btnConnection.Text = "Disconnect" Then
            CNN.Close()
            lblState.Text = "Disconnect"
            btnConnection.Text = "Connect to DB"
        Else
            CheckConnectionState()
        End If
    End Sub

Post a Comment

0 Comments