Ado. NET Programming Basics _ Practical skills

Source: Internet
Author: User

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.

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.