Differences and relationships between DataSet and DataTable in. Net

Source: Internet
Author: User

 

1. Briefly describe the relationship between the two
When we write code, we extract data from the database, fill it in the dataset, and then instantiate it into the able according to the table name. In fact, using dataset is equivalent to a copy of the data in the used database, which is stored in the memory of the server.
● Note the following Ado.net relationship:
Connection _____ Command ____ DataAdapter ____ Dataset _____ Datareader

● DataSet is stored in the memory. Modifications to data in DataSet are not directly reflected in the database. The data must be updated back to the database through the Update method of DataAdapter.

● DataSet is equivalent to the database you use; DataTable is equivalent to your table. One DataSet can contain multiple able

DataSet consists of tables, links, and constraints. In ADO. NET, the able object is used to represent tables in DataSet. DataTable indicates a table with in-memory relational data;

2. The following code example demonstrates how to construct the instance relationship of DataSet and DataTable.

 


[Vb]
Dim Ds As DataSet = New DataSet ("enUser ")

Dim Ds As DataSet = New DataSet ("enUser ")
The following example constructs a DataSet, adds a new able object to the DataSet, and then adds three DataColumn objects to the table. Finally, the Code sets a column as a primary key column.

 

[Vb]
Dim Ds As DataSet = New DataSet ("enUser ")
Dim ordersTable As DataTable = Ds. Tables. Add ("enUser ")
Dim pkCol As DataColumn = ordersTable. Columns. Add ("OrderID", Type. GetType ("System. Int32 "))
OrdersTable. Columns. Add ("OrderQuantity", Type. GetType ("System. Int32 "))
OrdersTable. Columns. Add ("CompanyName", Type. GetType ("System. String "))
OrdersTable. PrimaryKey = New DataColumn () {pkCol}
 

Dim Ds As DataSet = New DataSet ("enUser ")
Dim ordersTable As DataTable = Ds. Tables. Add ("enUser ")
Dim pkCol As DataColumn = ordersTable. Columns. Add ("OrderID", Type. GetType ("System. Int32 "))
OrdersTable. Columns. Add ("OrderQuantity", Type. GetType ("System. Int32 "))
OrdersTable. Columns. Add ("CompanyName", Type. GetType ("System. String "))
OrdersTable. PrimaryKey = New DataColumn () {pkCol}

 


3. Case sensitivity of Table1 and table1 in DataSet
Referencing tables and relationships in DataSet by name is case-sensitive. Two or more tables or relationships with the same name but different cases can exist in a DataSet. For example, Table1 and table1. In this case, the name-based reference of a table must exactly match the case sensitivity of the table name. Otherwise, an exception occurs. For example, if DataSet myDs contains tables Table1 and table1, Table1 is treated as myDS by name. tables ["Table1"] is referenced, and table1 is treated as myDS. tables ["table1. If you try to reference any of the Tables as myDS. Tables ["TABLE1"], an exception is generated.

If only one table or link with a specific name exists, the Case sensitivity rule is not applicable. That is to say, if no other table or relational object in DataSet matches the name of the specific table or relational object, the object can still be referenced by any case name even if the case is different, without exception. For example, if DataSet only contains Table1, you can use myDS. Tables ["TABLE1"] to reference it.

 

The above is a summary of the differences and links between DataSet and DataTable in. Net. If you have any shortcomings, please criticize and advise.

 

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.