Datasets and DataTable Explanations

Source: Internet
Author: User

A DataTable with the same structure as the user request data structure is built, then the user's request is populated into the built DataTable and the DataTable is added to the dataset.
Datatable,,datacolumn,datarow in-depth research

A DataTable is a relational data table in memory that can be created independently or used as a member of a DataSet!

How to use a DataTable as a member of a dataset:
Create a DataTable object first, and then add it to the Tables collection of the DataSet object by using the Add method
Example:
DataSet Dsclass = new DataSet (); Create a new empty class dataset
DataTable Dtclass = new DataTable ("Class"); Create a class table
DSCLASS.TABLES.ADD (Dtclass); Add a class table to a dataset
If you add a DataTable to a DataSet without specifying a DataTable name, the table gets a default table name that is incremented from "0"

The table you just started with does not have a table structure, and to define the structure of the table, you must create the DataColumn object and add it to the table's Columns collection. After a structure has been defined for a DataTable, the data is added to the Rows collection of the table through a DataRow object
DataColumn

DataColumn is the basis for creating a DataTable, defining the structure of a DataTable by adding one or more DataColumn objects to the DataTable. DataColumn has some common properties for restricting input data, such as: data type, data length, default value.

Common Properties of DataColumn
AllowDBNull whether null values are allowed
ColumnName stored data types
DataType stored data types
MaxLength Gets or sets the maximum length of a text column
Dafaultvalue Default Value
The name of the DataTable to which the Table belongs
Unique DataColumn is the only value

There are two ways to define DataColumn,
Method One:

//Create class Name columnDataColumn ClassName =NewDataColumn (); Classname.columnname="ClassName"; Classname.datatype= System.Type.GetType ("System.String"); Classname.maxlength= -;//Create class Name columnDataColumn ClassName =NewDataColumn ("ClassName",typeof(string)); Classname.maxlength= -//datarow//datarow represents the actual data contained in a DataTable, and we can add data to the DataTable defined in DataColumn with a DataRow. //Create class Name columnDataColumn ClassName =NewDataColumn ("ClassName",typeof(string)); Classname.maxlength= -;//Create a new row of dataDataRow Drclass =Dtclass.newrow ();d rclass["ClassName"] = This. TxtClassName.Text.Trim ();

How to customize a dataset
1. Create a DataSet object
2. Create a DataTable object
3. Create a DataColumn object build table Structure
4. Add the created table structure to the table
5. Create a DataRow object new data
6. Inserting data into a table
7. Adding a table to a dataset

Example:

//Create a new empty class datasetDataSet Dsclass =NewDataSet ();//Create a class tableDataTable Dtclass =NewDataTable ("Class");//Create class Name columnDataColumn Dcclassname =NewDataColumn ("ClassName",typeof(string)); Classname.maxlength= -;//Create a Grade ID columnDataColumn Dcgradeid =NewDataColumn (Gradeid", typeof (int));//add a well-defined column to the class tabledtClass.Columns.Add (dcclassname);d tClass.Columns.Add (Dcgradeid);//Create a new row of dataDataRow Drclass =Dtclass.newrow ();d rclass["ClassName"] = This. TxtClassName.Text.Trim ();d rclass["Gradeid"] = Objgrade.getgradeidbygradename ( This. CboGrade.Text.Trim ());//Insert a new data row into the class tableDtClass.Rows.Add (drclass);//add a class table to a datasetDSCLASS.TABLES.ADD (Dtclass);

How to get data in a dataset
There are two ways to get data from a dataset:
1. The first way is to obtain data by specifying a row of a specific DataTable in the dataset.
    steps:
        1. Get the specified DataTable from the dataset using the table name
        2. Gets the specified DataRow from the DataTable by index,
         3. Gets the data for the specified column from the DataRow by column name
Example:

 //  get class name  Dsclass.table[ " class  " ]. Row[0 ][ classname   " ];  //  get grade ID  dsclass.tables[ class  ]. Row[0 ][ gradeid  ] 

2. Another way is to bind the data in the dataset directly to the data presentation control.
We generally need to make some simple modifications to the data extracted from the dataset, such as hiding specific columns and sorting by a column. Quite simply,. NET provides us with a DataView object that can help us create different views of the data in the dataset, just like a view in a database. (a DataTable can dynamically generate multiple DataView)

DataView
DataView provides us with a dynamic view of the DataTable and the ability to sort, DA, and manipulate the data in a dynamically generated view, similar to a view in a database, except that it cannot provide a view of the associated DataTable, and it cannot exclude columns that exist in the original table. You cannot append a column that does not exist to the original table.

Example:

 //  filter out user status inactive learners  //  after filtered learner information is sorted in descending order of learner name  DataSet dsstudent = new   DataSet ();D ataview dvstudent  = new   DataView ();d vstudent.table  = Dsstudent.tables[ studenttable   ";d Vstudent.rowfilter  =  userstate = ' active '   " ;d vstudent.sort  =  studentname DESC    

DataView a few common properties:
Table is used to get or set the source DataTable
Sort Gets or sets one or more DataView and sort order
RowFilter Gets or sets an expression that filters which rows are viewed in DataView
Count gets the number of rows in DataView after applying RowFilter

Experience:
In development, if you need to dynamically filter or sort the data presented by Datagrigview, we'd better use the DefaultView (default view) property of the DataTable to get a view of the DataTable. This allows us to reduce the process of instantiating the DataView object and getting the DataView object's original DataTable.
DataTable dtstudent = (DataTable) Datagridview.datasource;
dtstudent.tables["Studenttable"]. Defaultview.rowfilter = "userstate= ' Activity '";

When we write the code, we extract the data from the database, populate it with the dataset, and instantiate it in the DataTable, based on the name of the table. In fact, using a dataset is equivalent to a copy of the data in the database being used and stored in the server's memory.
Note the following relationship with ADO:
Connection_____command____dataadapter____dataset _____datareader

The dataset is placed in memory, and changes to the data in the dataset are not directly reflected in the database, and are updated back to the database via the DataAdapter Update method

a dataset is quite the database you use; a DataTable is the equivalent of your table. A DataSet can contain multiple DataTable

A DataSet consists of a collection of tables, relationships, and constraints. In ADO, a DataTable object is used to represent a table in a dataset. A DataTable represents a table of in-memory relational data;

Case relationships of Table1 and table1 in datasets

Referencing tables and relationships in a dataset by name is case-sensitive. There can be two or more tables or relationships with the same name but different capitalization in a dataset. For example, you can have Table1 and table1. In this case, the by-name reference to one of the tables must exactly match the case of the table name, or an exception will be thrown. For example, if the DataSet myDs contains tables Table1 and table1, Table1 is referred to as myds.tables["Table1" by name and Table1 as MyD s.tables ["Table1"] to refer to. An exception is generated if you attempt to refer to any of these tables as Myds.tables ["TABLE1"].

A case-sensitive rule does not apply if there is only one table or relationship with a specific name. That is, if no other table or relational object in the DataSet matches the name of that particular table or relationship object, even if the case is different, the object can be referenced by any case name, without throwing an exception. For example, if the DataSet contains only Table1, you can use myds.tables["TABLE1" to refer to it.

Datasets and DataTable Explanations

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.