I. ADO. NET Basics
Program and database interaction, to be done through the ado.net, through the ado.net can execute SQL in the database. Ado. NET provides a unified operational interface (ODBC) for different databases. Another interface for manipulating databases is JDBC.
Ado. NET in the SqlConnection class to create a link to SQL Server, SqlConnection represents a database of links, ADO. NET, the IDisposable interface is implemented by resources such as links.
The object that implements the IDisposable interface is used up, and the resource is released. Call the Dispose () method.
Connect the way to access the database, the connection to open, use the end to close. After closing, you can open it again, which is the difference between close and dispose. Dispose () is a full release of resources.
Now let's test to see if the connection to the database is successful.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
With Ado.net technology, you must import the two namespaces
using System.Data;
Using System.Data.SqlClient;
namespace ADO. NET1
{
class program
{
static void Main (string[] args)
{
//CREATE database connection (connection string)
String Sqlcon = "server=.; Database=db_mystudentlife;uid=sa;pwd=password_1 ";
SqlConnection scon = new SqlConnection (Sqlcon);
Open the database connection
Scon. Open ();
Test to determine the connection status of the database
if (Scon). state = = connectionstate.closed)
{
Console.WriteLine ("Connection Database Failed");
}
if (Scon. state = = ConnectionState.Open)
{
Console.WriteLine ("Successfully open connection database, successful connection");
}
Console.readkey ();}}
Test Effect Diagram:
Finally, let's look at the internal principle of the SqlConnection object: Use the Reflector tool to do it;
1. From the diagram, we can see that SqlConnection inherits the DbConnection class and implements the ICloneable interface
2.ICloneable interface inside what it looks like, let's take a look at, decompile:
3. Next, let's look at the DbConnection class:
As can be seen from the diagram, the DbConnection class is an abstract class, inherits the component class, implements the IDbConnection interface, and the IDisposable interface, let's look at these several
4.Component class:
We can see that this class, also implements the IDisposable interface Oh,
Well, let's look at the mystery inside the IDisposable interface:
It can be seen that idisosable defines a Dispose () that abstracts a method that has no return value.
The above mentioned is the entire content of this article, I hope you can enjoy.