using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace provaLetturadb1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnRiempiList_Click(object sender, EventArgs e) { try { OleDbConnection oc = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../../Dati/Northwind16.accdb;"); oc.Open(); //apre la connessione string istrSql = ""; istrSql += "SELECT * FROM Prodotti ORDER BY NomeProdotto"; OleDbCommand comando = new OleDbCommand(istrSql, oc); OleDbDataReader dr = comando.ExecuteReader(); // esegue la query listBox1.Items.Clear(); listBox2.Items.Clear(); listBox3.Items.Clear(); while (dr.Read()) { listBox1.Items.Add(dr[0].ToString()); listBox2.Items.Add(dr["NomeProdotto"].ToString()); int w = int.Parse(dr["IDProdotto"].ToString()); // IDProdotto è numerico (intero) listBox3.Items.Add(w); } dr.Close(); //chiude il datareader dr = null; oc.Close(); //chiude la connection oc = null; } catch (Exception ex) { MessageBox.Show("Errore " + ex.Message); }