当前位置:网站首页>Using to release resources

Using to release resources

2022-06-24 18:39:00 MousseIn

understand using The role of

Use Connection, How to ensure that the connection is closed

We used to use try…catch…final The way , Put the database connection statement inside , stay final Write a close statement in a statement block .

Use using Release Connection object

  • using One of the usages of : Import namespace
  • Release the resources occupied by the object , such as :Connection、DataReader、FileStream wait
    How to write it :

using( type Object name = new type ( parameter list ))

//…
//…

Braces are declarations using The effective range of , In parentheses is the scope of application of the object , Once out of this scope of use , The program will automatically release its internal objects .

using(SqlConnection conn = new SqlConnection(connString))
{
    
conn.open();
//......
}

There is no need to explicitly call Close() Method .
Examples are as follows :

#region bage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
#endregion
namespace CH03
{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            string sql = "select count(*) from dbo.Grade";
            string conStr = "server=.;database=MySchool;uid=sa;pwd=123456";
            int count = 0;
            SqlConnection con = null;

            using( con = new SqlConnection(conStr))
            {
    
               
                con.Open();
                SqlCommand cmd = new SqlCommand(sql,con);
                 count = Convert.ToInt32(cmd.ExecuteScalar());
                //con.Close();
            }
            Console.WriteLine(con.State);// Check whether the execution status is closed 
            Console.WriteLine(count);
            Console.ReadLine();
        
        }
    }
}

Use using Release SqlDataReader

using(sqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
    
while(reader.Read())
{
    
// Traverse query results 
}
}

CommandBehavior.CloseConnection It's a guarantee DataReader Automatically release after release Connection.
Examples are as follows :

#region bage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
#endregion
namespace CH03
{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            //string sql = "select count(*) from dbo.Grade";
            string sql = "select * from dbo.Grade";
            string conStr = "server=.;database=MySchool;uid=sa;pwd=123456";
           // int count = 0;
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            using(SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))// Close by the way SqlConnection object 
            {
    
                while (reader.Read())
                {
    
                    Console.WriteLine(reader[0]+" "+reader[1]);
                }
            }
            //using( con = new SqlConnection(conStr))
            //{
    
               
            // con.Open();
            // SqlCommand cmd = new SqlCommand(sql,con);
            // count = Convert.ToInt32(cmd.ExecuteScalar());
            // //con.Close();
            //}
            Console.WriteLine(con.State);// Check whether the execution status is closed 
           // Console.WriteLine(count);
            Console.ReadLine();
        
        }
    }
}

using The essence of

Equivalent to the try-catch-finally Of finally The object name is called in the block .Dispose() Method .

  • Dispose() yes IDisable Interface method
  • Dispose() Dedicated to releasing scarce resources of objects
  • object Class implements the Dispose(), Some classes implement Close()
  • Close() Encapsulates the Dispose() Call to

Master the use of using Release resource objects

原网站

版权声明
本文为[MousseIn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211332353878.html