Deepen Understanding and understanding of ADO. net

Source: Internet
Author: User
Ado. Net has two basic parts: DataSet and managed provider. The following is a brief overview of dataset's common methods.
Dataset is a database in memory that provides consistent Program Design the model, regardless of where it comes from. Dataset consists of a group of tables, columns, rows, constraints, and relationships. The object model for dataset is as follows:

A data table (able) is a memory data table. It contains a column set (columnscollection) that represents the table's schema ). A data table also contains a rowscollection, which indicates the data owned by the table. It remembers the initial status and current status, and tracks the changes that have taken place.
To use a data table, you must include system. Data.

Create a data table

Datatable has two constructors:
Public datatable ()
Public datatable (string tablename)

Add columns to a data table
Datatable contains a collection of datacolumn objects. This column set defines the structure of the table. To add a new column to this set, you can use the add method of this set.

In the following example, we use the Add method of the columnscollection class to add three columns to a data table. This method specifies the columnname and ype attributes.

1 Datacolumn DC =   Null ;
2 Datatable dt =   New Datatable ( " Test " );
3 DC = DT. Columns. Add ( " Custid " , System. type. GetType ( " System. int32 " ));
4 DC = DT. Columns. Add ( " Customernamelast " , System. type. GetType ( " System. String " ));
5 DC = DT. Columns. Add ( " Customernamefirst " , System. type. GetType ( " System. String " ));
6 DC = DT. Columns. Add ( " Purchases " , System. type. GetType ( " System. Double " ));

The add method of columnscollection on the able has two overload functions:
Public datacolumn add (string columnname, type)
Public datacolumn add (string columnname)

Expression Column

Ado. Net also allows you to create and define expression columns. Expressions in ADO + are used for filtering, computing, and summarizing column information.
To create an expression column, you must set the datatype attribute to the type suitable for the value returned by the expression. Then, set the expression attribute to a valid expression:
Datacolumn Dc = new datacolumn;
DC. datatype = system. type. GetType ("system. Currency ");
DC. Expression = "Total *. 086 ";

You can also use the add method to create an expression column. For exampleCodeAdded a column to calculate the Discount Based on 10% of the customer's purchase amount. This expression times the column "purchases" by 10%.
Datacolumn Dc = new datacolumn;
Dc = DT. Columns. Add ("rectg", system. type. GetType ("system. Double"), "Total * 0.1 ");
When the table is added with data, the value of this column will be 10% of the value in the total column.

Auto increment Column
Another feature of datacolumn is its ability to act as an automatic incremental column. When a new column is added, the value in the column is automatically added. To create an automatic incremental column, you must set the autoincrement attribute of this column to true (true ). Once this attribute is set, the value defined in the autoincrementseed attribute of the column is used at the beginning of the column. After a column is added, the values of the auto increment column are incremented by the values in the autoincrementstep attribute of the column.
Dc = DT. Columns. Add ("custid", system. type. GetType ("system. int32 "));
DC. autoincrement = true;
DC. autoincrementseed = 1;
DC. autoincrementstep = 1;

Create a primary keyword for the table
DT. primarykey = new datacolumn [] {dt ["custid"]};

Add data to a table

Now there is a table with columns and keywords, you can add some data.
Datarow dtrow = NULL;
For (INT I = 0; I <= 9; I ++)
{
Dtrow = DT. newrow ();
Dtrow [0] = I;
Dtrow [1] = "custname" + I. tostring ();
DT. Rows. Add (dtrow );
}

Column status

Each data row (datarow) has a rowstate attribute, which can be used to determine its status.

In the preceding example, the rowstate attribute of the new row is set to detached (separated). Once you add it to the rowscollection using the add method, the rowstate attribute is changed to new.

Rowstate description
Unchanged has not changed since the last call of acceptchanges.
The new row has been added to the table, but acceptchanges has not been called.
Some elements of the modified row have been changed.
The delete method has been used to delete the row from the table.
The detached or row has been deleted, but acceptchanges has not been called; or the row has been created but has not been added to the table.

Delete or remove a data row from a table

When you process data in a table, you sometimes need to remove one or more rows from the table. However, you must note that removing a row is different from deleting a row as deleted. Removing a row physically removes the row from the dataset. This difference is important when we discuss how to maintain values in tables or data storage.
To remove a row from a table, you can use its index or actual instance to call the Remove Method of the table rowscollection.

DT. Rows. Remove (3 );
After this method is called, the specified row is removed from the row set.

DT. Rows [3]. Delete
Mark a row as deleted. When the acceptchanges method of the able object is called, the row will be removed from the table. Therefore, the rowstate attribute of the row is deleted. Conversely, if you call the rejectchanges method, the rowstate attribute of this row is restored to the State before it is marked as deleted.

Filter table data
Datarow [] currrows = DT. Select (condition );
The select method allows you to search rows based on three elements: Filter expressions, sorting order, and dataviewstate.

 

Dataset datset

The dataset provides two basic constructor functions.
Public dataset ()
Public dataset (string datasetname)

Add a data table to a dataset

Datatable dt = new datatable ("user ");
Dataset DS = new dataset ();
DS. Tables. Add (DT );

Add a link between two tables
Since a dataset (Dataset) can contain multiple able objects, you must have methods to associate tables with each other. This is required to navigate between tables and return related values.
The basic independent variable of datarelation is the primary key and external key columns in the relationship, and the name of datarelation. This name can be used to navigate or retrieve numeric values.
DS. relations. Add (...);
Dr = new datarelation (...);
DS. relations. Add (DR );

Managed provider

Command, connection, and datareader
Command, connection, and datareader represent the core elements of the ADO model. Connection knows how to connect to a specific data storage. Microsoft provides two connection objects in the ngws framework: sqlconnection and adoconnection. Sqlconnection knows how to connect to a Microsoft SQL Server database.

Set table and column ing
Workdscmd. tablemappings. Add ("table", "myauthors"); // table ing
Workdscmd. tablemappings [0]. columnmappings. Add ("au_id", "authorid"); // column ing

There are many other ways to review the materials today and write them down as a future document!

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.