Connection e DataAdapter




Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;"
Or you can use an ODBC DSN, As you can see from the following code that I've used TestDSN DSN
to connect to the data source:
Dim conn As New OdbcConnection("DSN=TestDSN")
To test this code, create a Windows application And add a DataGrid control To the form And write
code listed In Listing 1 On the form load. As you can see In Listing this code Is similar To the
code you saw earlier. It creates a connection, a data adapter, fills the dataset from the data adapter,
and sets the dataset's DefaultViewManager as the DataGrid control's DataSource property.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim connectionString As String = "Driver={MySQL};SERVER=localhost;DATABASE=NorthwindMySQL;" '
Dim conn As New OdbcConnection(connectionString)
conn.Open()
Dim da As New OdbcDataAdapter("SELECT CustomerID, ContactName, ContactTitle FROM Customers", conn)
Dim ds As New DataSet("Cust")
da.Fill(ds, "Customers")
dataGrid1.DataSource = ds.DefaultViewManager
conn.Close()
End Sub 'Form1_Load












( connectionedataadapter.html )- by Paolo Puglisi - Modifica del 17/12/2023