2010-06-26 13 views
11

Jak utworzyć DataSet, który jest wypełniany ręcznie? to znaczy. wypełniać kod lub wprowadzać dane przez użytkownika. Chcę znać wymagane kroki, jeśli muszę najpierw utworzyć DataTable lub DataRow, naprawdę nie wiem, jakie kroki należy wykonać, aby wypełnić DataSet.Dodawanie wierszy do zestawu danych

Odpowiedz

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

Następnie po wykonaniu wszystkich tych czynności, co należy zrobić, aby dodać wiersz do już istniejących DataTable w zbiorze? – 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"]));  
} 
Powiązane problemy