Torna al Thread

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.Collections; namespace Project4 { class TestParameters { private DataSet dsDati= new DataSet(); private string cnText= @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\stage\db.mdb";//stringa di connessione private void GeneraDataSet() { OleDbConnection cn = new OleDbConnection(cnText); OleDbCommand cm = new OleDbCommand("SELECT * FROM ANAGRAFICA", cn); OleDbDataReader dr; // ovviamente si tratta del database di sql server, ma con // un altro database funziona allo stesso modo try { cn.Open(); // molto importante se si vogliono ottenere anche le informazioni // sulle chiavi del database dr = cm.ExecuteReader(CommandBehavior.KeyInfo); // GetSchemaTable restituisce un oggetto datatable con le // informazioni relative alla tabella aperta dal datareader DataTable schemaTable = dr.GetSchemaTable(); if (schemaTable != null) { // un arraylist per conservare I dati delle chiavi primarie ArrayList pkCols = new ArrayList(); // creiamo una “normale” tabella per I dati DataTable dTable = new DataTable("ANAGRAFICA"); //dTable.TableName = "MARCHE"; // cicliamo sulle righe della tabella schema foreach (DataRow schemaRow in schemaTable.Rows) { // ad ogni riga corrisponde una nuova colonna DataColumn col = new DataColumn(); col.ColumnName = schemaRow["ColumnName"].ToString(); col.DataType = (Type)schemaRow["DataType"]; // set the length of the field for string types only if (schemaRow["DataType"].ToString() == "System.String") col.MaxLength = (Int32)schemaRow["ColumnSize"]; col.Unique = (bool)schemaRow["IsUnique"]; col.AllowDBNull = (bool)schemaRow["AllowDBNull"]; col.AutoIncrement = (bool)schemaRow["IsAutoIncrement"]; // If part of the key, add the column name to the // array of columns comprising the primary key. if ((bool)schemaRow["IsKey"]) pkCols.Add(col); dTable.Columns.Add(col); } // aggiungiamo la chiave primaria if (pkCols.Count > 0) dTable.PrimaryKey = (DataColumn[])pkCols.ToArray(typeof(DataColumn)); // Aggiungiamo la tabella al DataSet. dsDati.Tables.Add(dTable); // ci prepariamo per leggere tutte le righe object[] aData = new object[dTable.Columns.Count]; // leggiamo tutte le righe dal datareader while (dr.Read()) { //recupero dei dati su un array dr.GetValues(aData); // Aggiunta riga alla tabella dTable.Rows.Add(aData); } dr.Close(); // questo corrisponde ad AcceptChangesDuringFill del dataadapter dsDati.AcceptChanges(); } int indice=0; while (indice < dsDati.Tables[0].Rows.Count-1) { System.Console.Write(dsDati.Tables[0].Rows[indice]["codice anagrafico"] + " ");/////riga che mi da il problema... System.Console.WriteLine(dsDati.Tables[0].Rows[indice]["ragione sociale"]); indice++; } } catch (Exception ex) { Console.Write(ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (cn.State == ConnectionState.Open) cn.Close(); } } public static void Main() { TestParameters t = new TestParameters(); t.GeneraDataSet(); Console.Write("finito"); } } }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5