日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
DataReader的關(guān)閉問題疑惑篇

昨天一個朋友使用Repeater綁定數(shù)據(jù)源時,老是出現(xiàn)"閱讀器關(guān)閉時嘗試調(diào)用 FieldCount 無效。"錯誤。

創(chuàng)新互聯(lián)IDC提供業(yè)務(wù):達州主機托管,成都服務(wù)器租用,達州主機托管,重慶服務(wù)器租用等四川省內(nèi)主機托管與主機租用業(yè)務(wù);數(shù)據(jù)中心含:雙線機房,BGP機房,電信機房,移動機房,聯(lián)通機房。

我看了他的代碼,使用的是SqlHelper類下面的ExecuteReader方法,返回一個SqlDataReader進行綁定。

 
 
  1. public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)      
  2. {  
  3.     SqlCommand cmd = new SqlCommand();  
  4.     SqlConnection conn = new SqlConnection(CONN_STRING);  
  5.     // we use a try/catch here because if the method throws an exception we want to 
  6.     // close the connection throw code, because no datareader will exist, hence the  
  7.     // commandBehaviour.CloseConnection will not work 
  8.     try  
  9.     {  
  10.         PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);  
  11.         SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
  12.         cmd.Parameters.Clear();  
  13.         return rdr;  
  14.     }  
  15.     catch  
  16.     {  
  17.         conn.Close();  
  18.         throw;  
  19.     }  

然后,他自己又編寫了一個類,GetData,用其中的一個方法來調(diào)用SqlHelper.

 
 
  1. public SqlDataReader GetReader()  
  2. {  
  3.     using (SqlDataReader reader = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "GetAllUser", null))  
  4.     {  
  5.         return reader;  
  6.     }  

其中GetAllUser是一個不帶參數(shù),查詢所有用戶的存儲過程,***通過返回的reader進行數(shù)據(jù)源綁定。

他出現(xiàn)的這個錯誤,明顯是在讀取reader之前,reader已經(jīng)被關(guān)閉(using的原因)。因此會讀不到數(shù)據(jù),就會報那種錯誤。因此,把他的調(diào)用方法改了一下:

 
 
  1. public SqlDataReader GetReader()  
  2. {  
  3.     try  
  4.     {  
  5.         SqlDataReader reader=SqlHelper.ExecuteReader(CommandType.StoredProcedure, "GetAllUser", null);  
  6.         return reader;  
  7.     }  
  8.     catch (Exception)  
  9.     {  
  10.         return null;  
  11.     }  

這樣,就不會報錯了,UI層進行數(shù)據(jù)綁定也沒有任何問題。但是,隨之一個新的問題出現(xiàn)了:SqlDataReader沒有手動關(guān)閉,會不會造成連接池達到***值?

我們注意到,在SqlHelper里面,使用到了CommandBehavior.CloseConnection,這個的作用是"關(guān)閉SqlDataReader 會自動關(guān)閉Sqlconnection." 也就是說,關(guān)閉Sqlconnection是在關(guān)閉SqlDataReader的前提下進行,還是需要手動關(guān)閉SqlDataReader。又要返回SqlDataReader,又要關(guān)閉它,怎么辦呢?有的網(wǎng)友提出:在return reader之前,先關(guān)閉reader,即進行reader.Close();

實際上這樣是不行的,會報與前面一樣的錯誤,在讀取數(shù)據(jù)之前,已經(jīng)被關(guān)閉。

CommandBehavior.CloseConnection用于關(guān)閉數(shù)據(jù)庫連接,這是肯定的,但它會不會一起把SqlDataReader也一起關(guān)閉了呢。也就是說,用了CommandBehavior.CloseConnection,是不是就不用再手動關(guān)閉SqlDataReader了。

我們中以使用SqlHelper,然后在前臺網(wǎng)頁里面進行測試

 
 
  1. protected void bind()  
  2. {  
  3.     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());  
  4.     conn.Open();  
  5.     SqlCommand cmd = new SqlCommand("GetAllUser", conn);  
  6.     SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
  7.     repeater1.DataSource = sdr;  
  8.     repeater1.DataBind();  
  9.     Response.Write(sdr.IsClosed.ToString()+"
    ");  
  10.     Response.Write(conn.State.ToString());  

輸出的結(jié)果是

?True

?Closed

?說明SqlConnection和SqlDataReader都已經(jīng)被關(guān)閉了。

?如果把CommandBehavior.CloseConnection去掉,輸出的結(jié)果則是:

?False

?Open

?由此可見,使用了CommandBehavior.CloseConnection之后,讀取完數(shù)據(jù)后,系統(tǒng)自動關(guān)閉了SqlDataReader和SqlConnection。聽說當初微軟弄出CommandBehavior.CloseConnection的目的,就是為了解決數(shù)據(jù)庫的關(guān)閉問題的。

?當然,我的數(shù)據(jù)量非常少,不能說明問題。希望更多的朋友說說自己的想法。


標題名稱:DataReader的關(guān)閉問題疑惑篇
轉(zhuǎn)載源于:http://www.dlmjj.cn/article/cohesdp.html