Ado. Net has two connection modes: No connection mode and connection mode. The relationship between them is as follows:
1. If we regard the database as a large pool, connection is the tap that is put into the water and plays a key role. Only when it is connected can we draw water, that is to say, data can be extracted from the database. It is a connection object. It provides four types of database access objects:
1. SQL Server databaseProgramIn the namespace of system. Data. sqlclient;
2. ODBC data provider, located in the system. Data. ODBC namespace;
3. oledb data provider, located in the namespace of system. Data. oledb;
4. The Oracle Data Provider is located in the namespace of system. Data. oracleclient.
When using connection, we usually have to call the open object to open the database. After use, you need to close the database. In this case, the close () and dispose () objects can be used to close the database. The differences between the two are:The close method is used to close a connection, while the dispose method not only closes a connection, but also clears the resources occupied by the connection.. When close () is used to close the connection, you can call the Open Method to open the connection without generating an error. If you use the dispose method to close the connection, you cannot use the open method to open the connection again, the connection must be reinitialized and then enabled.
2. The command object is like a pumping machine. It provides power and execution methods for pumping, and uses the "faucet" to reverse the water to the above pipe. It is a data command object. Its main function is to send SQL statements for queries, updates, deletions, and modifications to databases. Command objects include sqlcommand, oledbcommand, odbccommand, and oraclecommand );
The command object has three SQL statements:
1. excutenonquery method-execute an SQL statement that does not return a dataset and return the affected number of rows. When SQL command is used to send an add, delete, or modify command to the database, it is usually used to execute the SQL statement sent.
2. executereader method-execute an SQL statement and generate an instance of the sqldatareader object containing data. The returned value is a sqldatareader object.
3. executescalar method-execute an SQL statement and return the first column in the first row of the result set. The return value is usually the first column or null value in the first row of the result set (if the result set is empty ).
3. The dataadapter and datareader Objects Act as water transmission tasks. And act as a bridge. The dataadapter object is like a pipe, which uses an engine to send water to a reservoir for storage. The datareader object is also a type of water pipe. Unlike the dataadapter object, it does not send water to the reservoir, but directly delivers water to users who need water, so it is faster than turning around in the reservoir.
1. the datareader object is a data reader object that provides a cursor that is read-only. If the application needs to retrieve the latest data from the database each time, or only needs to read the data quickly, it does not need to modify the data, you can use the datareader object to read data. Different types of datareader objects are available for different connections.
To read data from a data table, use the executereader method. For each associated sqlconnection, only one sqldatareader can be opened at a time. Before the first one is closed, any attempt to open another will fail.
2. The dataadapter object is a data adapter object that serves as a bridge between dataset and the data source and is used for intercommunication with the data source. It provides the following four attributes:
1>. sqlcommand attribute: used to send query SQL statements to the database;
2>. deletecommand attribute: used to send a delete SQL statement to the database;
3>. insertcommand attribute: used to send query SQL statements to the database;
4>. updatacommand attribute: Send an update statement to the database;
There are several important methods in dataadapter:
1>. Fill dataset with data in the fill method.
Fill the dataset with the fill method of the dataadapter object. The connection object associated with the SELECT command must be valid, but you do not need to open it. Dataset objects are like small databases stored in memory. It can contain data tables, data columns, data rows, views, constraints, and relationships.
2> update the database. (In this case, dataadapter calls the deletecommand, insertcommand, and updatecommand attributes );
You can use the updata method of the dataadapter object to update the modified data in dataset to the database in a timely manner.
My friend told me that the learning is still well written, and I can record the difficulties I encountered during the learning process and the methods for solving the problems at any time. I will take a note of every knowledge point. I personally think it is more feasible. It is better than I have been confused once. Hey hey, so I got this note and will write it again later... As the content in this article is all typed by your own hand, it is not copied or pasted, so please forgive me for any write errors. Thank you...