SoFunction
Updated on 2025-03-07

In C#, use the Connection class to implement code instances to open/close databases

In order to access the database, a database connection class must be provided. In C#, it is implemented through the Connection class.

Four types of connection methods

  1. SQLConnection
  2. ADOConnection
  3. OractleConnection
  4. ODBCConnection

Implement database connection in SQLConnection:

  • SQL Server Database
  • Windows Identity Information Verification

step:

  1. Reference namespace using;
  2. Put the connection method declaration value in the string
  3. Create a Connection object
  4. Calling methods

In order to save system resources and improve system performance, it is best to close the connection after using the database. In C# language, the existence of GC (garbage collection mechanism) will release resources at a certain time in the future. It is non-decisive and cannot be sure when this process occurs. When forgetting to close the database, you can use the statement to ensure that the object exits immediately, thereby achieving the effect of closing the database. There is also a way to close the database throughtry..catch..final..Statement controls the shutdown of the connection database to improve performance

The code implementation is as follows:

using System;
using ;  //Introduce namespacenamespace Csharpone
{
  class Program
  {
    static void Main(string[] args)
    {
      //Windows Identity Information Verification The following csharp01 is the newly created database name      string constr = "Server.;integrated security=SSPI;Initial Catalog=csharp01";
      SqlConnection mysqlCon = new SqlConnection(constr); //Instantiation      (); //Open the database      ("Database Open");  //There is no problem with the normal printing instructions, otherwise an exception will be thrown      //SQ verification method name is the user name of the database you set, pwd is the password csharp02 is the database name      string constr1 = "Server.;user=name; pwd=mima; database=csharp02";
      SqlConnection mysqlCon1 = new SqlConnection(constr1); //Instantiation      (); //Open the database      ("SQL method database open");
 /*Can close the database through using statement
       using (mysqlCon1) {
         ();
         ("Data is opened successfully"); //Close immediately after execution
       }
       //Through try..catch..finally..
       try
       {
         ();
         ("Database Close");
       }
       catch
       {
       }
       Finally
       {
         ();
         ("Close the database");
       }*/
//Use the above two methods in combination to ensure that the database occupies resources are released      try
      {
        using (mysqlCon)
        {
          ();
   ("Open Database");
        }
      }
      catch
      {
      }
      finally
      {
        ();
     ("Close the database");
      }
      ();
    }
  }
}

The MySQL database code is as follows:

using System;
using ;  //Import the reference and add the namespacenamespace CSharpconnectMysql
{
  class Program
  {
    static void Main(string[] args)
    {
      string connectStr = "server=localhost;port=3306;database=czhenya01;user=root;password=123456;";
      //No database connection was established      MySqlConnection conn = new MySqlConnection(connectStr);
      try
      {
        ();  //Create a connection and open the database        ("The database is successfully opened");
      }catch (Exception ex)
      {
        (());
      }
      finally
      {
        ();  //Close the connection      }      
      ();
    }
  }
}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links