2010-06-26 17 views
11

Come posso creare un DataSet che viene riempito manualmente? vale a dire. riempi il codice o l'input dell'utente. Voglio sapere i passaggi necessari se ho bisogno di creare un DataTable o un DataRow prima, davvero non conosco i passaggi per riempire il DataSet.Aggiunta di righe al set di dati

risposta

42
DataSet ds = new DataSet(); 

DataTable dt = new DataTable("MyTable"); 
dt.Columns.Add(new DataColumn("id",typeof(int))); 
dt.Columns.Add(new DataColumn("name", typeof(string))); 

DataRow dr = dt.NewRow(); 
dr["id"] = 123; 
dr["name"] = "John"; 
dt.Rows.Add(dr); 
ds.Tables.Add(dt); 
+0

Poi, dopo tutti questi passaggi che cosa devo fare per aggiungere la riga alla già esistente DataTable all'interno di set di dati? – sam

4
DataSet myDataset = new DataSet(); 

DataTable customers = myDataset.Tables.Add("Customers"); 

customers.Columns.Add("Name"); 
customers.Columns.Add("Age"); 

customers.Rows.Add("Chris", "25"); 

//Get data 
DataTable myCustomers = myDataset.Tables["Customers"]; 
DataRow currentRow = null; 
for (int i = 0; i < myCustomers.Rows.Count; i++) 
{ 
    currentRow = myCustomers.Rows[i]; 
    listBox1.Items.Add(string.Format("{0} is {1} YEARS OLD", currentRow["Name"], currentRow["Age"]));  
} 
Problemi correlati