Ado. NET Learning Notes (ii)

Source: Internet
Author: User
ado| notes 4, DataSet
The purpose of a dataset in Ado.net is to provide a disconnected store for the data source without having to care about the data source, which is done only in a dataset.
There are three ways to create Dataset:1, through DataAdapter 2, through XML file 3, by manually determining the schema, and then entering data row by line.
The first method is mainly introduced.
DataAdapter is used to connect a dataset to a basic data store, which is essentially a meta command object.
It includes SelectCommand objects, InsertCommand objects, UpdateCommand objects, DeleteCommand objects.
Template code:
Dim Dataadpater as New SqlDataAdapter ("select * from Student", conn)
Dim DataSet as New DataSet ()
DataAdapter.Fill (DataSet)
In this case the table name of the dataset defaults to table
If a batch query is used and the resulting results are filled in the dataset, the table name defaults to Table,table1,table2 ...

TableMappings:
Table Name Mapping:
Table name Mapping after generating DataAdapter
DATAADAPTER.TABLEMAPPINGS.ADD ("Table", "Customer")
DataAdapter.Fill (DataSet)
The alias of the table becomes a Customer (can be used with a table or a customer operation on the DataSet), and Dataset.tables ("Customer") can refer to the table
Or
DATAADAPTER.TABLEMAPPINGS.ADD ("ADONet", "Customer")
DataAdapter.Fill (DataSet, "adonet")
Column Name Mapping:
DATAADAPTER.TABLEMAPPINGS.ADD ("Table", "Customer")
Dataadapter.tablemappings ("Customer"). Columnmappings.add ("CustomerID", "ID")
DataAdapter.Fill (DataSet, "Customer")

schemas, adding schemas through FillSchema
1. Add primary key
Customertable.primarykey=new datacolumn[]{customertable.columns ("CustomerID")} (add primary key by array)

2. Add relationship
DATASET.RELATIONS.ADD ("Customers_invoices", Dataset.tables ("Customers"). Columns ("CustomerID"), Dataset.tables ("Invoinces"). Columns ("CustomerID"), true)

3, add constraints
There are two main constraints: Unique constraint, outer code constraint (uniqueconstraint,foreignkeyconstraint)
Where the unique constraints are divided into DeleteRule (cascade delete constraints), UpdateRule (cascading update constraints), AcceptRejectRule (constraints when invoking AcceptChanges or RejectChanges)

4. Add triggers
You can add triggers to the 6 dataset event
rowchanging,rowchanged,columnchanging,columnchanged,rowdeleting,rowdeleted

5, column structure
For example, add a column's properties: Customertable.columns ("CustomerID"). Readonly=true
Or add AutoIncrement columns:
Customertable.columns ("CustomerID"). Autoincrement=true
Customertable.columns ("CustomerID"). Autoincrementseed=1 (column starting position)
Customertable.columns ("CustomerID"). Autoincrementstep=1 (column increment step)
Maybe some people will say that all of this in the DBMS do not do it, why so much trouble in the ado.net to the dataset to write again?
This is mainly due to efficiency considerations, if the client's error input can be found on the client, but not to the server to verify the words can reduce the unnecessary transmission.

Expression columns:
Dim Excolumn as New DataColumn ("LineTotal")
Excolumn.datatype=typeof (float)
Excolumn.expression= "((price-(Price*discount)) *quantity)"
Dataset.tables ("items"). Columns.Add (Excolumn)




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.