Scritto in Visual Studio 2003


codice:
Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form

'qui codice grafico per la form

    Private ConnectionString As String
    Private Connection As OleDb.OleDbConnection
    Private Command As OleDb.OleDbCommand

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cmdText As String
        cmdText = "SELECT * FROM ComboValues"
        Dim oDr As OleDb.OleDbDataReader = GetDataReader(cmdText)

        'Ora posso usare il dataread per, ad esempio popolare la mia combobox
        'RICORDARSI DI CHIUDERLO, e di chiudere la connessione
        While (oDr.Read())
            ComboBox1.Items.Add(oDr("valori")) 'valori è il nome del campo della tabella ComboValues che contiene i valori edlla combo
        End While

        oDr.Close()
        Connection.Close()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;OLE DB Services=-2;Data Source=db.mdb;"
        Connection = New OleDb.OleDbConnection(ConnectionString)

    End Sub

    Private Function GetDataReader(ByVal cmdText As String) As OleDb.OleDbDataReader

        Dim RetValue As OleDb.OleDbDataReader
        Connection.Open()
        Command = New OleDb.OleDbCommand(cmdText, Connection)
        RetValue = Command.ExecuteReader()
        Return RetValue
    End Function

End Class