Basic knowledge of ADO. NET programming, ado.net Programming

Source: Internet
Author: User

Basic knowledge of ADO. NET programming, ado.net Programming

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.

Access the database through the connection method. The connection must be enabled and closed after use. You can enable it again after closing, which is the difference between Close and Dispose. Dispose () is to completely release resources.

Now let's test whether the database connection is successful.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; // use 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 a database connection (connection string) string sqlCon = "server = .; database = DB_MyStudentLife; uid = sa; pwd = Password_1 "; SqlConnection scon = new SqlConnection (sqlCon); // open the database connection to scon. open (); // test to determine the database connection status if (scon. state = ConnectionState. closed) {Console. writeLine ("failed to connect to Database");} if (scon. state = ConnectionState. open) {Console. writeLine ("successfully opened the connection to the database, successfully connected");} Console. readKey ();}}}

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.

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.