ADO. NET Learning Series (1) and ado.net Learning Series
I. Basics of ADO. NET
- The program interacts with the database through ADO. NET, and the SQL statement can be executed in the database through ADO. NET. ADO. NET provides a unified operation interface (ODBC) for different databases ). Another interface for database operations is JDBC.
- In ADO. NET, a link is created to SQL Server through the SqlConnection class. Sqlconnection represents a database link. resources such as links in ADO. NET all implement the IDisposable interface.
- Objects that implement the IDisposable interface must be released after use. Call the Dispose () method.
Now let's test whether the database connection is successful.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 // use ADO. NET technology, you must import these two namespaces 7 using System. data; 8 using System. data. sqlClient; 9 // 10 namespace ADO. NET111 {12 class Program13 {14 static void Main (string [] args) 15 {16 // create a database connection (connection string) 17 string sqlCon = "server = .; database = DB_MyStudentLife; uid = sa; pwd = Password_1 "; 18 SqlConnection scon = new SqlConnection (sqlCon); 19 20 // open the database connection 21 scon. open (); 22 23 // test to determine the database connection status 24 if (scon. state = ConnectionState. closed) 25 {26 27 Console. writeLine ("failed to connect to Database"); 28} 29 if (scon. state = ConnectionState. open) 30 {31 Console. writeLine ("successfully opened the database connection, connection successful"); 32} 33 Console. readKey (); 34} 35} 36}Test whether the database connection is successfully enabled. If yes, the system prompts "successful". Otherwise, the system prompts "failed ".
Test:
Finally, let's take a look at the internal principle of the SqlConnection object: Use the decompilation tool reflector;
1. We can see that SqlConnection inherits the DbConnection class and implements the ICloneable interface.
2. What does the ICloneable interface look like? Let's take a look at the decompilation:
3. Next, let's look at the Dbconnection class:
It can be seen that the Dbconnection class is an abstract class that inherits the Component class and implements the IDbConnection interface and the IDisposable interface. Let's take a look at the several
4. Component class:
This class also implements the IDisposable interface,
Let's take a look at the secrets inside the IDisposable interface:
It can be seen that IDisosable defines a Dispose () method that abstracts methods without return values.