【原文地址:http://www.cnblogs.com/agian/articles/1648835.html】html
其用在ExecuteReader(c)中,返回對象前不能關閉數據庫鏈接,須用CommandBehavior.CloseConnection;面試
這是一個關於實際知識點的問題,面試官考查的是應聘者數據庫訪問的編程經驗。本節將針對這個問題展開具體的分析。對於此類關於具體知識點的問題,讀者在平時應該注意積累,這樣在面試中才能從容應答。數據庫
所涉及的知識點編程
CommandBehavior.CloseConnection的使用測試
分析問題htm
因爲流模式讀取數據庫的特色,在具體應用時很難肯定數據庫鏈接什麼時候才能被關閉,由於讀取的動做是連續進行的,下面是一個常見的數據訪問層的靜態方法:
/// <summary>對象
/// 常見的獲取SqlDataReader方法blog
/// 一般的數據訪問層都會提供這個方法字符串
/// </summary>get
static SqlDataReader GetReader()
{
//經過鏈接字符串獲取鏈接
SqlConnection con = new SqlConnection(conn_String);
try
{
//打開鏈接,執行查詢
//而且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
finally
{
//這裏的代碼處於兩難的境地
//若是這裏執行關閉:con.Close();那返回的
SqlDataReader將毫無用處,由於其
//依賴的鏈接已經關閉
//若是這裏不執行con.Close();那返回後該鏈接
將永遠沒法關閉,由於調用方沒法
//獲得鏈接對象
}
}
正如代碼註釋裏描述的那樣,這樣的方法既不能關閉鏈接,也不能保持鏈接打開狀態。不少系統爲了解決這樣兩難的境地,只能放棄使用Reader模式的數據源,或者把鏈接對象交給方法調用者,以便進行關閉。
而CommandBehavior.CloseConnection的功能剛好就是爲了不相似的尷尬境地,它可以保證當SqlDataReader對象被關閉時,其依賴的鏈接也會被自動關閉。代碼9-2展現了使用CommandBehavior.CloseConnection和不使用CommandBehavior.CloseConnection的區別。
這裏以SqlDataReader爲例進行說明,對於其餘命名空間下的XXXDataReader對象,其功能是相似的。
首先爲了展現功能,代碼9-2包含了兩個靜態的返回SqlDataReader的方法,其中一個在執行ExecuteReader方法時傳入了CommandBehavior.CloseConnection方法。
代碼9-2 使用CommandBehavior.CloseConnection:UseCommandBehavior.cs
partial class UseCommandBehavior
{
//數據庫看鏈接字符串
const String conn_String =
"Server=localhost;Integrated Security=true;database=NetTest";
const String Sql = "select * from dbo.DepartCost";
/// <summary>
/// 使用CommandBehavior.CloseConnection
/// </summary>
/// <param name="con">爲了測試須要,傳入鏈接對象</param>
static SqlDataReader GetReader_CloseConnection(SqlConnection con)
{
try
{
//打開鏈接,執行查詢
//而且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader
(CommandBehavior.CloseConnection);
return dr;
}
finally
{
//由於使用了CommandBehavior.CloseConnection,
//這裏不須要關閉鏈接
//con.Close();
}
}
/// <summary>
/// 不使用CommandBehavior.CloseConnection
/// </summary>
/// <param name="con">爲了測試須要,傳入鏈接對象</param>
static SqlDataReader GetReader_NoCloseConnection(SqlConnection con)
{
try
{
//打開鏈接,執行查詢
//而且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
finally
{
//爲了使返回的SqlDataReader可用,這裏不能關閉鏈接
//con.Close();
}
}
}
能夠看到,不管是否使用CommandBehavior.CloseConnection,兩個方法都沒有在最終關閉鏈接,可是它們不關閉鏈接的緣由並不相同。準備好了兩個方法以後,就從主方法中分別調用這兩個方法來進行測試,以查看從使用了CommandBehavior.CloseConnection的方法中返回的SqlDataReader對象是否在關閉的同時自動關閉鏈接,如代碼9-3所示。
代碼9-3 使用CommandBehavior.CloseConnection:UseCommandBehavior.cs
partial class UseCommandBehavior
{
/// <summary>
/// 測試方法
/// </summary>
static void Main(string[] args)
{
//創建鏈接
SqlConnection con = new SqlConnection(conn_String);
try
{
//測試使用了CommandBehavior.CloseConnection的方法
Console.WriteLine("測試使用了CommandBehavior.
CloseConnection的方法:");
SqlDataReader sdr = GetReader_CloseConnection(con);
while (sdr.Read()) { }
sdr.Close();
Console.WriteLine("讀取完畢後的鏈接狀態:" + con.State.ToString());
//測試沒有使用CommandBehavior.CloseConnection的方法
Console.WriteLine("測試沒有使用CommandBehavior.
CloseConnection的方法:");
SqlDataReader sdr1 = GetReader_NoCloseConnection(con);
while (sdr1.Read()) { }
sdr1.Close();
Console.WriteLine("讀取完畢後的鏈接狀態:" +
con.State.ToString());
Console.Read();
}
finally
{
//確保鏈接被關閉
if (con.State != ConnectionState.Closed)
con.Close();
}
}
}
下面是代碼的執行結果:
測試使用了CommandBehavior.CloseConnection的方法:
讀取完畢後的鏈接狀態:Closed
測試沒有使用CommandBehavior.CloseConnection的方法:
讀取完畢後的鏈接狀態:Open
正如讀者所看到的,使用了CommandBehavior.CloseConnection獲得的SqlDataReader對象,在關閉的同時會自動地關閉其依賴的數據庫鏈接對象,這個特性解決了數據訪問層編寫中的困境。
答案
CommandBehavior.CloseConnection解決了流讀取數據模式下,數據庫鏈接不能有效關閉的狀況。當某個XXXDataReader對象在生成時使用了CommandBehavior.CloseConnection,那數據庫鏈接將在XXXDataReader對象關閉時自動關閉。