自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

DataReader的關(guān)閉問題疑惑篇

數(shù)據(jù)庫 SQL Server 數(shù)據(jù)庫運(yùn)維
使用Repeater綁定數(shù)據(jù)源時(shí),老是出現(xiàn)"閱讀器關(guān)閉時(shí)嘗試調(diào)用 FieldCount 無效。"錯(cuò)誤。使用了CommandBehavior.CloseConnection之后,讀取完數(shù)據(jù)后,系統(tǒng)自動(dòng)關(guān)閉了SqlDataReader和SqlConnection。

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

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

  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.     }  

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

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

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

他出現(xiàn)的這個(gè)錯(cuò)誤,明顯是在讀取reader之前,reader已經(jīng)被關(guān)閉(using的原因)。因此會(huì)讀不到數(shù)據(jù),就會(huì)報(bào)那種錯(cuò)誤。因此,把他的調(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.     }  

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

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

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

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

我們中以使用SqlHelper,然后在前臺(tái)網(wǎng)頁里面進(jìn)行測(cè)試

  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()+"<br/>");  
  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)自動(dòng)關(guān)閉了SqlDataReader和SqlConnection。聽說當(dāng)初微軟弄出CommandBehavior.CloseConnection的目的,就是為了解決數(shù)據(jù)庫的關(guān)閉問題的。

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

原文鏈接:http://www.cnblogs.com/denny402/archive/2011/04/05/denny.html

 

責(zé)任編輯:艾婧 來源: 博客園
相關(guān)推薦

2011-04-07 09:40:57

DataReader鏈接關(guān)閉

2011-04-28 11:04:22

DataReader分頁

2011-09-14 09:58:18

云計(jì)算

2023-09-01 07:25:39

領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)DDD

2009-09-03 10:26:07

C#修改DataRea

2009-11-04 12:45:33

ADO.NET Dat

2020-07-09 08:42:23

jvm內(nèi)部緩存

2011-04-14 09:42:06

DataReaderDataSet

2009-10-29 11:08:20

ADO.NET Dat

2019-09-24 10:02:57

Jvm內(nèi)部緩存

2013-05-21 10:10:29

Hadoop文件系統(tǒng)

2010-07-15 15:21:25

AIX telnet

2009-07-30 10:00:28

WinForm動(dòng)態(tài)菜單

2014-08-28 14:16:05

錘子測(cè)評(píng)

2009-11-13 10:31:07

ADO.NET Dat

2022-03-21 15:11:17

Java繼承初始化

2011-04-29 08:46:58

C#代碼規(guī)范

2010-03-23 09:16:43

Python源碼剖析

2011-12-12 09:46:46

2016-09-19 18:57:25

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)