hanane
hanane

Reputation: 1

VB - Connection to a data base

I had a training in computer development in 2008. I learned to code in VB. At the time, our trainer told us how to connect VB with a database under access. We work in offline mode and therefore use the dataSet.

I want to know the difference between these two ways of connecting to a database on VB:

  1. Enter the code manually (under module.vba):
    -> Connection Statement (Public Exp. Cn as new oledbConnection), OledbCommand, OledbDataAdapter, dataSet, ...
    -> Manually initialize the connectionString, the command texts, fill the tables of the dataSet, ...

  2. Add the DataSource to the project?

Upvotes: 0

Views: 163

Answers (1)

try to write this code.

1- add new item (class), rename it and modify it to be like this:

public class ConnectDB
*
*
*
End Class

after that you tape some code inside.

Imports System.Data.SqlClient
Public Class ConnectDb
    Public ServerName As String
    Public Dbname As String
    Public usrSQL As String
    Public PwdSQL As String

    Dim Conn As New SqlConnection("Data Source=" + ServerName + "; Initial Catalog=" + Dbname + "; User Id =" + usrSQL + "; Password=" + PwdSQL + ";")

    Sub OpenCn()
        If Conn.State = ConnectionState.Closed Then
            Conn.Open()
        End If
    End Sub

    Sub CloseCn()
        If Conn.State <> ConnectionState.Closed Then
            Conn.Close()
        End If
    End Sub

End Class

this is the block code for connection .. same concept as connecting to MS Access but instead of SQLConn , you will put OLEConnection and same as command too (OLECommand).. Plus, some difference in the under quotes in the connection string .. also , you should import the library named:

Imports System.Data.OleDb

instead of

Imports System.Data.SqlClient

Upvotes: -1

Related Questions