C#datatable Learning Experience

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.