C#datatable Learning Experience
I. Datasets, DataTable, DataRow, DataColumn
1 "Adding a DataTable to a dataset
DATASET.TABLES.ADD (DataTable)
Instance:
DataSet ds=new DataSet ();
DataTable table=new DataTable ("Student Table");
Ds. Tables.add (table); 2 "Reading a DataTable from a dataset
Datatable=dataset.tables[0] or dataset.tables["table name"]
Instance:
DataTable Table=ds[i] or DataTable table=ds["Student Table"]
I is the index value in the dataset, because the dataset can hold more than one DataTable, you can find a DataTable based on the index value or write the DataTable name directly to find a DataTable 3 "Add row DataTable t=new DataTable (); DataRow R=t.newrow (); r["column name"]= column value; T.rows.add (R);
Instance:
DataTable: Student Table
ID Name
1 Xun
Code:
DataTable t=new DataTable ("Student Table");
DataRow R=t.newrow ();
r["id"]=2;
r["Name"]=XUN2;
T.rows.add (R);
4 "Add column
DATATABLE.COLUMNS.ADD ("Column name", Type.GetType ("Data type")); 5 "Reading column values from rows
datarow["column name"] or Datarow[datacolumn]; 6 "Reading column values from a DataTable
DataTable table;
A, table. rows[i]["column name"]
b, table. Rows[i][i]
C, table[i]. Column names (column names are not quoted)
7 "read out a specific line
DataTable table;
Datarow[] selectrow=table. Select ("Column name = '" + holds a specific variable.) ToString () + "'");
Select one of the rows: selectrow[index]
Ii. Delete rows in a DataTable three methods: (DataTable.Rows.Remove (DataRow Dr), DataTable.Rows.RemoveAt (i), Datarow.delete ())
Delete Rows in a DataTable to pay attention to index problems, there are generally two ways: 1 "with a For loop, note that the counter initial value is the table length, self-reduction loop. DataTable.Rows.RemoveAt (i) should be noted. 2 "With the Select method of the DataTable, note that the parameter of the method is string filter 3" Delete () after a DataTable is required. The Acceptechanges () method confirms a complete deletion, because delete () only flags the status of the corresponding column as deleted, and it can be passed through a DataTable. RejectChanges () rollback to make the row undelete. To delete multiple rows, you can use Delete () consecutively, and then use the Acceptechanges () method to confirm the deletion.
C#datatable Learning Experience