using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Management; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public const String TABELLA = "test1"; private System.Data.OleDb.OleDbDataReader oleDataReader; public Form1() { InitializeComponent(); } public void Form1_Load(object sender, EventArgs e) { object[] obj = new object[100]; try { String str_query_ricerca = "SELECT * FROM" + " " + TABELLA; obj = connetti_db(str_query_ricerca, "test.udl"); } catch { MessageBox.Show("Attenzione: query di ricerca errata!!"); } //ciclo lettura dati foreach (Object element in obj) { if (element != null) MessageBox.Show(element.ToString()); else continue; } } private object[] connetti_db(String stringa_query, String nome_udl) { System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("File Name=" + nome_udl); string str_result; object[] obj=new object[1000]; Object[] result = new Object[100000]; Boolean read; int j = 0; try { //instaura connessione a db string sql = stringa_query; //apri connessione conn.Open(); //esegui query su db selezionato System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(sql, conn); oleDataReader = command.ExecuteReader(); Object[] tmp = new Object[oleDataReader.FieldCount]; //ciclo lettura records if (oleDataReader.Read() == true) { do { int NumberOfColums = oleDataReader.GetValues(tmp); //scansione valori dei singoli campi del record. for (int i = 0; i < NumberOfColums; i++) { result[j] = tmp[i]; //copio in vettore che ritorno j++; } read = oleDataReader.Read(); } while (read == true); } } catch(Exception e) { MessageBox.Show("Attenzione: impossibile eseguire query!!"); } return result; } } }