同桌上课用手指进去了好爽_欧美丰满熟妇xxⅹⅹ性大i_成人av天天日天天拍拍_猛男gay帅男gay男男同志_欧美va天堂在线观看_人妻无码av中文系列三里桃花_亚欧免费无码在线观看_久久久精品国产亚洲av水_日韩在线免费看污污污_2021无码专区人妻系列日韩

首頁 優(yōu)化推廣 C#中釋放數(shù)據(jù)庫連接資源

C#中釋放數(shù)據(jù)庫連接資源

來源: | 時間:2011/2/19 6:28:58 |

1.確保釋放數(shù)據(jù)庫連接資源的兩種方式如下:
  a.使用try...catch...finally語句塊,在finally塊中關(guān)閉連接;
  b.使用using語句塊,無論如何退出,都會自動關(guān)閉連接;
2.最好的方法是組合使用以上兩種方式。

 

using System;
using System.Data.SqlClient;

namespace Magci.Test.DataAccess
{
   
class Program
   
{
      
static void Main(string[] args)
      
{
          
string source = @"server=.\sqlexpress; integrated security=SSPI; database=msdb";
         
SqlConnection conn = null;
         
//使用try塊處理
         
try
          
{
              
conn = new SqlConnection(source);
             
conn.Open();
             
//Do something
         
}
         
catch (Exception e)
        
{
             
//Do something with the exception
          
}
          
finally
          
{
              
conn.Close();
          
}

 
         
//使用using塊處理
          
using (conn = new SqlConnection(source))
          
{
               
conn.Open();
               
//Do something
          
}
 
 
           
//兩種組合方式
          
try
          
{
              
using (conn = new SqlConnection(source))
             
{
                  
conn.Open();
                  
//Do something
                  
conn.Close();
             
}
          
}
          
catch (Exception e)
          
{
             
//Do something with the exception
         
}
      
}
   
}
}

上一篇:C#中split用法

下一篇:cstr用法

服務(wù)熱線

153 8323 9821

功能和特性

價格和優(yōu)惠

網(wǎng)站和維護(hù)

推廣和優(yōu)化

微信服務(wù)號