UnTyped Dataset and Strong-Typed Dataset

Source: Internet
Author: User

Take the table name "MyTable" and the field FirstName varchar (30) and FamilyName varchar (30) as an example.

Non-strong (UnTyped) Dataset does not need to define the attributes of each field of the corresponding table in advance, you can directly obtain the values from the queried result set (non-strong (UnTyped) Dataset), for example:String lFirstName = LDs. Table ["MyTable"]. Rows [0] ["FirstName"]. ToString ();(LDs is an object of Dataset class)
A Strong (Strong-Typed) Dataset needs to pre-define the attributes of each field in the corresponding table (the attributes must be inherited from DataSet, DataTable, and DataRow to generate the MyDataset, MyDataTable, and MyDataRow classes ), for example:String lFirstName = lTypedDs. MyTable [0]. FirstName;
(WhereLTypedDsIs an object like MyDataset;
LTypedDs. MyTableIs an object of the MyDataTable class, which is an attribute of the MyDataset class;
LTypedDs. MyTable [0]Returns a MyDataRow object, which is a specific definition class of an object.EntityObject(Including the FirstName and FamilyName attributes), corresponding to the Schema of the MyTable table;
LTypedDs. MyTable [0]. FirstNameIs a String type, which is an attribute of MyDataRow)

The approximate pseudocode of this strong type is as follows (you can use the XSD corresponding to the MyDataTable table in the IDE environment to automatically generate the full code of the strong type Dataset: MyDataset through XSD ):

Public class MyDataset: DataSet
{

Public MyDataset ()
{
This. MyTable = new MyDataTable ();
}

Public class MyDataTable: DataTable
{
 
Internal DataColumn columnFirstName;
Internal DataColumn columnFamilyName;
Internal MyDataTable ():
Base ("MyTable ")
{
This. columnFirstName = new DataColumn ("FirstName", typeof (string), null, System. Data. MappingType. Element );
This. Columns. Add (this. columnFirstName );
This. columnFamilyName = new DataColumn ("FirstName", typeof (string), null, System. Data. MappingType. Element );
This. Columns. Add (this. columnFamilyName );

}
Public MyDataRow this [int index]
{
Get {
Return (MyDataRow) (this. Rows [index]);
}
}

}
 
Public class MyDataRow: DataRow
{
Internal MyDataRow (DataRowBuilder rb ):
Base (rb ){
This. tableMyTable = (MyDataTable) (this. Table ));
}
Private MyDataTable tableMyTable;

Public string FirstName
{
Get {
Return (string) (this [this. tableMyTable. columnFirstName]);
}
Set {
This [this. tableMyTable. columnFirstName] = value;
}
}
Public string FamilyName
Get {
Return (string) (this [this. tableMyTable. columnFamilyName]);
}
Set {
This [this. tableMyTable. columnFamilyName] = value;
}
}
Public MyDataTable MyTable;
}
Comparison between the two:

UnTyped Dataset: Strong-Typed Dataset:
The types of attributes of object objects cannot be defined:
LDs. Table ["MyTable"]. Rows [0] ["FirstName"] cannot be defined
You can define the types of attributes of an object:
MyDataRow. FirstName is of the string type.
Static conventions cannot be set during Coding, but can only be set through TableName FieldName:
String lFirstName = lDs. Table ["MyTable"]. Rows [0] ["FirstName"]. ToString ()
Coding allows static conventions:
String lFirstName = lTypedDs.MyTable [0].FirstName
Custom method attributes cannot be provided because there is no inheritance Because the custom Dataset, DataTable and DataRow can add other method attributes in addition to the method attributes of the parent class.
Verification (type conversion and spelling check) is not allowed during compilation. You can only check whether the system is correct or wrong at runtime. For example, you cannot determine whether "MyTable" or "FirstName" is misspelled. It can be verified during the compilation period (whether the types are matched or not ).

Are you still using lDs. Table ["MyTable"]. Rows [0] ["FirstName"]. ToString () programming? Do you really hate these ugly code? Don't worry. Use strong Dataset to make our code pleasing to the eye.

In a word:A Strong-type (Strong-Typed) Dataset never (or almost) occurs in our code in this form: 

Tables ["TableName"]. Rows [n] ["FieldName"]

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.