First, ADO. NET Introduction
Ado. NET is a class library of many classes that provide many objects to perform operations such as connection, query, insert, UPDATE, and delete of data. The main objects include the following:
1. Connection object: Used to connect to the database (OLE DB using Oledbconnection,sql server using SqlConnection)
2. Command object: Used to execute SQL commands against the database, such as inserting, deleting, modifying, querying
3. DataReader object: Used to return read-only data from the database
4. DataAdapter object: Used in conjunction with DataSet object to realize control of database
5. DataSet object: Can be considered as an in-memory database
Second, the method of reading the database from ADO
Ado. NET provides a class library (specifically for accessing SQL Server databases)
The namespaces that are imported in the code-behind file are:
Usying System.Data;
Usying System.Data.SqlClient;
Third, the use of the core object method
(i) SqlConnection object
Step one Create connection object and set connection string
SqlConnection conn=new SqlConnection ("Data sourse= ...; Initial catalog=.; ")
Step two open the database connection using the Open method
Conn. Open ();
Step three operations Database
Step four close the database connection
Conn. Close ();
II) SqlCommand object
Step one create the SqlCommand object and insert the data
SqlCommand cmd= new SqlCommand ();
String sql= "Insert into......values (...)";
Step two execute the command by method
int
< P align= "left" method name |
return type |
|
executenonquery |
Executes SQL and returns the number of rows affected |
executescalar |
object |
Executes SQL and returns the first row of the first column of data |
executereader |
sqldatareader |
Returns a read-only data flow object |
Database connection use--connect to the database using ADO