Leggere file Access con VB.NET
In questo articolo vediamo come aprire e leggere dati da un file Access usando VB.NET.
Prima di tutto dobbiamo installare tramite NuGet il pacchetto System.Data.OleDb.
Fatto questo possiamo eseguire la connessione:
Imports System
Imports System.Data.OleDb
Module Program
Sub Main(args As String())
Dim desktop As String = "C:\Users\Pc\Desktop\"
Dim mdb As String = desktop + "MAGA.accdb"
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + mdb)
Dim cmd As String = "SELECT * FROM Articolo"
Dim command As OleDbCommand = New OleDbCommand(cmd, connection)
Try
connection.Open()
Using reader = command.ExecuteReader()
While reader.Read()
Console.WriteLine("{0} {1}", reader("Codice").ToString(), reader("prezzo vendita").ToString())
End While
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
End Module
Gli ogetti che ci interessano sono:
- OleDbConnection per la connessione
- OleDbCommand per eseguire la query
- OleDbDataReader per leggere e scorrere il risultato della query
Enjoy!
vbnet nuget oledb access
Commentami!