Use of DataTable objects

Source: Internet
Author: User
Tags add object modify tostring

A DataTable represents a data table in memory, not a table in a database. Any changes to the DataTable will not affect the database until it is persisted to the database.
The DataAdapter.Update method can be used to persist to a database.
The DataTable objects include:
1. Multiple DataColumn objects
2. Multiple DataRow objects.
To create a DataTable object first
DataTable table1=new DataTable ();
To add a Column object to a DataTable object
DataColumn Coluserid = new DataColumn ("user_id", Type.GetType ("System.int"));
Table1. Columns.Add (colcurrency);
The system type name of the column: Because the DataTable is an in-memory table and not a table in the database, its DataColumn data type is the system data type. Can be obtained in the following two ways:
typeof (System type name) settings such as:
DataColumn C1 = new DataColumn ("id", typeof (int));
Type.GetType ("System. Type name") setting
DataColumn C2 = new DataColumn ("name", Type. GetType ("System.String"));
DataColumn DC2 = new DataColumn ();
DC2. AllowDBNull = false; NULL is not allowed
DC2. MaxLength = 10; The maximum length is 10
DC2. Unique = true; Name column does not repeat
Table1. PrimaryKey = new datacolumn[] {DC1};//setting primary key is ID
datarow[] rows = table1. Select ("id>=1000 and name Like '");//Find
Table1.              AcceptChanges (); Take Back Roll
Response.Write (table1. Rows[0]. Rowstate.tostring ());
Table1.     rows[0]["Name" = "Leslie"; Write-Modify statements between AcceptChanges () and RejectChanges ()
Response.Write (table1. Rows[0]. Rowstate.tostring ());
Table1. RejectChanges (); Rolling back
Table1. rows[1]["Name" = "Guan Yu";//Modify
Table1. ROWS[2].        Delete (); Delete, delete only state do not delete data
Table1.        Rows.removeat (2); Delete, delete completely



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.