Document directory
- ADO. NET Architecture
- Connection provider class
- DataSet
- DataAdapter
In this chapter, we mainly introduce the Data Access Technology in. NET: ADO. NET.
ADO. NET Architecture
We can see that there are two types of classes in ADO. NET:
- 1. Connection provider class (Connection, Command, DataReader, etc)
- 2. Data classes (DataSet, DataTable, DataView, etc)
- These two categories are consistent with the component Categories I introduced in the previous chapter.
Connection provider class
Different databases use different providers to access the database. NET Framework to publish a set of public interfaces, allowing database vendors to implement their own providers. The built-in data providers include:
1. System. Data. SqlClient (access SQL Server)
2. System. Data. OracleClient (access to Oracle, But Oracle itself provides a better Privider called ODP. NET, which is recommended for direct use)
3. System. Data. OleDb (use the OleDb protocol to access the database)
4. System. Data. Odbc (Use Odbc protocol to access the database)
In. NET1.1, the Provider model such as System. Data. Sqlclient. SqlConnection is used to implement System. Data. IDbConnection.
In. in NET2.0, the Provider Model of abstract factory reality is used. for example, System. data. sqlclient. sqlConnection inherits from System. data. common. dbConnection, while System. data. common. dbConnection implements System. data. IDbConnection.
By using the factory, we can easily write common data access code without considering the subsequent database. in the following example. data. sqlClient exists in the configuration file and is read at runtime:
Var factory = System. Data. Common. DbProviderFactories. GetFactory ("System. Data. SqlClient ");
Var conn = factory. CreateConnection ();
Var cmd = factory. CreateCommand ();
Var cmbBuilder = factory. CreateCommandBuilder ();
Var adp = factory. CreateDataAdapter ();
Var parm = factory. CreateParameter ();
...
To know which providers are registered on your machine, you can call this access method:
DataTable registeredProviders = System. Data. Common. DbProviderFactories. GetFactoryClasses ();
DataSet
In short, DataSet is a database in memory. it supports repeated data access and updates. xml-based data storage makes it easy to serialize/deserialize Xml data. dataSet, a type of data access with a large granularity, is suitable for distributed systems. another advantage is that it is based on the "Collection" data structure, which greatly simplifies the compilation of client programs.
Note that DataSet (and DataTable, DataColumn) has an ExtendedProperties attribute, which is essentially a dictionary. you can store custom attributes, such as the DataSet generation date and expiration date.
For example:
Ds. ExtendedProperties. Add ("Expiredate", DateTime. Now. AddHours (8 ));
Ds. ExtendedProperties. Add ("Author", "Rock Niu ");
DataAdapter
How to pull the data in DataSet from the DB and how to update the data back to the DB depends on the DataAdapter structure:
When constructing a DataAdapter, we usually need to specify SelectCommand, which is used to query data in DataAdapter. Fill (DataSet.
The Update, Insert, and Delete commands only call the DataAdapter. update (DataSet) is used only. They can be automatically generated by CommanBuilder. however, when it is dynamically generated, the metadata of the table will be accessed, and all columns in the Command will be placed in the where condition, which will affect the performance. we recommend that you specify the Text of the Update, Insert, and Delete commands. It is best to use the stored procedure, which has the best performance.