Come creare una DataTable in C#?
Ho fatto così:
DataTable dt = new DataTable();
dt.clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
Come faccio a vedere la struttura di DataTable?
Ora voglio aggiungere ravi per Nome
e 500 per Marks
. Come posso farlo?
Ecco il codice:
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
DataRow _ravi = dt.NewRow();
_ravi["Name"] = "ravi";
_ravi["Marks"] = "500";
dt.Rows.Add(_ravi);
Per vedere la struttura, o meglio la riformulerei come schema, potete esportarla in un file XML facendo quanto segue.
Per esportare solo lo schema/struttura, fai:
dt.WriteXMLSchema("dtSchemaOrStructure.xml");
Inoltre, puoi anche esportare i tuoi dati:
dt.WriteXML("dtDataxml");
Dovete aggiungere datarows al vostro datatable per questo.
// Creates a new DataRow with the same schema as the table.
DataRow dr = dt.NewRow();
// Fill the values
dr["Name"] = "Name";
dr["Marks"] = "Marks";
// Add the row to the rows collection
dt.Rows.Add ( dr );