无码国产精成人午夜视频不卡,久久久无码精品亚洲日韩蜜桃 ,国产亚洲欧美日韩在线一区 ,狠狠色丁香婷婷综合久久来来去

新聞建站cms系統(tǒng)、政府cms系統(tǒng)定制開發(fā)

廣州網(wǎng)站建設公司-閱速公司

asp.net新聞發(fā)布系統(tǒng)、報紙數(shù)字報系統(tǒng)方案
/
http://www.szzsmy888.com/
廣州網(wǎng)站建設公司
您當前位置:首頁>ASP.NET MVC

ASP.NET MVC

asp.net中SqlDataReader使用時關(guān)閉數(shù)據(jù)庫連接的問題

發(fā)布時間:2010/2/26 11:15:38  作者:  閱讀:1839  

廣告:

本文解釋使用SqlDataReader關(guān)閉數(shù)據(jù)庫連接的問題:

例如把數(shù)據(jù)庫的操作都封裝到了一個類中,但SqlDataReader只有在讀取完畢時才能關(guān)閉數(shù)據(jù)庫,這樣類中就不能關(guān)閉書庫庫連接。在函數(shù)中關(guān)閉,如果在函數(shù)中就關(guān)閉了會提示‘閱讀器關(guān)閉時Read的嘗試無效’ .

這點微軟當然想到了。用著個方法dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);,MSDN中對 CommandBehavior.CloseConnection的解釋是‘在執(zhí)行該命令時,如果關(guān)閉關(guān)聯(lián)的 DataReader 對象,則關(guān)聯(lián)的 Connection 對象也將關(guān)閉。’

但。。。。。,請看下面的問題:

在函數(shù)中執(zhí)行操作然后返回DataReader,然后在外面讀出數(shù)據(jù),讀取完畢后在外部關(guān)閉DataReader,這樣函數(shù)中的Connetion會自動關(guān)閉嗎?

實例展示:

我把所有對數(shù)據(jù)庫的操作都封裝到了一個類中,寫程序時就直接調(diào)用這個類的某個函數(shù)來返回結(jié)果!(我們學習網(wǎng)-weareleran.net)

下面是一個用來返回DataReader對象的通用類, public SqlDataReader GetReader(string SQL) { SqlConnection Conn; Conn = new SqlConnection(strConn); Conn.Open(); SqlCommand Cmd ; Cmd = CreateCmd(SQL, Conn); SqlDataReader dr; try { dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { throw new Exception(SQL); } Cmd.Dispose(); return dr; }

在程序在需要的時候我就直接構(gòu)造好SQL語句,調(diào)用這個函數(shù)即可,如下:

string sql="select * form student"; SqlDataReader dr = mydate.GetReader(sql);//調(diào)用數(shù)據(jù)庫操作通用類中的GetReader函數(shù),返回SqlDataReader if(dr.Read()) { .........讀出數(shù)據(jù),賦值給頁面控件..... } dr.Close();//操作完畢,關(guān)閉DataReader

我按照這樣的寫基本已經(jīng)沒問題了,可以正常讀取出數(shù)據(jù),但是我現(xiàn)在擔心的一個問題是Connection對象的關(guān)閉問題,因為是在函數(shù)中建立的 Connection對象,沒辦法在外部來關(guān)閉它,又不能在函數(shù)中關(guān)閉,如果在函數(shù)中就關(guān)閉了會提示‘閱讀器關(guān)閉時Read的嘗試無效’

但我在函數(shù)中就用了這一行:dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);,MSDN中對 CommandBehavior.CloseConnection的解釋是‘在執(zhí)行該命令時,如果關(guān)閉關(guān)聯(lián)的 DataReader 對象,則關(guān)聯(lián)的 Connection 對象也將關(guān)閉。’

也就是說我在關(guān)閉了DataReader之后,關(guān)聯(lián)的Connection會自動關(guān)閉,我也測試過,測試代碼如下:

string strConn = ConfigurationSettings.AppSettings["SqlDatabase"]; SqlConnection cn=new SqlConnection(strConn); cn.Open(); string sql="select * form student"; SqlCommand cm=new SqlCommand(sql,cn); SqlDataReader dr=cm.ExecuteReader(CommandBehavior.CloseConnection); SqlDataReader dr = mydate.RunProcGetReader(sql); if(dr.Read()) { .......讀取數(shù)據(jù) } dr.Close();//關(guān)閉DataReader Response.Write(cn.State);//輸出Connetion的狀態(tài)
結(jié)果運行這一段,輸出:Closed ,可見Connetion確實自動關(guān)閉了!但是如我在上面所說,我的Coonection是在函數(shù)在建立的,在函數(shù)中執(zhí)行操作然后返回 DataReader,然后在外面讀出數(shù)據(jù),讀取完畢后在外部關(guān)閉DataReader,這樣函數(shù)中的Connetion會自動關(guān)閉嗎

我把所有對數(shù)據(jù)庫的操作都封裝到了一個類中,寫程序時就直接調(diào)用這個類的某個函數(shù)來返回結(jié)果!

下面是一個用來返回DataReader對象的通用類,

public SqlDataReader GetReader(string SQL) { SqlConnection Conn; Conn = new SqlConnection(strConn); Conn.Open(); SqlCommand Cmd ; Cmd = CreateCmd(SQL, Conn); SqlDataReader dr; try { dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { throw new Exception(SQL); } Cmd.Dispose(); return dr; }
在程序在需要的時候我就直接構(gòu)造好SQL語句,調(diào)用這個函數(shù)即可,如下:

string sql="select * form student";

SqlDataReader dr = mydate.GetReader(sql);//調(diào)用數(shù)據(jù)庫操作通用類中的GetReader函數(shù),返回SqlDataReader

if(dr.Read())
{
.........讀出數(shù)據(jù),賦值給頁面控件.....
}
dr.Close();//操作完畢,關(guān)閉DataReader



我按照這樣的寫基本已經(jīng)沒問題了,可以正常讀取出數(shù)據(jù),但是我現(xiàn)在擔心的一個問題是Connection對象的關(guān)閉問題,因為是在函數(shù)中建立的 Connection對象,沒辦法在外部來關(guān)閉它,又不能在函數(shù)中關(guān)閉,如果在函數(shù)中就關(guān)閉了會提示‘閱讀器關(guān)閉時Read的嘗試無效’

但我在函數(shù)中就用了這一行:dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);,MSDN中對 CommandBehavior.CloseConnection的解釋是‘在執(zhí)行該命令時,如果關(guān)閉關(guān)聯(lián)的 DataReader 對象,則關(guān)聯(lián)的 Connection 對象也將關(guān)閉。’

也就是說我在關(guān)閉了DataReader之后,關(guān)聯(lián)的Connection會自動關(guān)閉,我也測試過,測試代碼如下:

string strConn = ConfigurationSettings.AppSettings["SqlDatabase"]; SqlConnection cn=new SqlConnection(strConn); cn.Open(); string sql="select * form student"; SqlCommand cm=new SqlCommand(sql,cn); SqlDataReader dr=cm.ExecuteReader(CommandBehavior.CloseConnection); SqlDataReader dr = mydate.RunProcGetReader(sql); if(dr.Read()) { .......讀取數(shù)據(jù) } dr.Close();//關(guān)閉DataReader Response.Write(cn.State);//輸出Connetion的狀態(tài)

結(jié)果運行這一段,輸出:Closed ,可見Connetion確實自動關(guān)閉了!但是如我在上面所說,我的Coonection是在函數(shù)在建立的,在函數(shù)中執(zhí)行操作然后返回 DataReader,然后在外面讀出數(shù)據(jù),讀取完畢后在外部關(guān)閉DataReader,這樣函數(shù)中的Connetion會自動關(guān)閉嗎?

自己做了個測試:(我們學習網(wǎng)-www.wearelearn.net)

public SqlConnection conn; protected void Page_Load(object sender, EventArgs e) { conn = new SqlConnection(DAL.SQLHelper.conn_String); show(); } //簡單的封裝方法 protected static SqlDataReader reDatareader(SqlConnection conn) { SqlCommand cmd = new SqlCommand("select top 10 * from article ", conn ); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return dr; } //應用封裝方法 protected SqlDataReader redr() { SqlDataReader dr = reDatareader(conn); return dr; } //調(diào)用類,模擬3層構(gòu)架。 protected void show() { SqlDataReader dr = redr(); if (dr.HasRows) { while (dr.Read()) { } Response.Write(conn.State.ToString()); dr.Dispose(); Response.Write(conn.State.ToString()); } }
結(jié)果輸出 OpenClosed,呵呵,結(jié)果能關(guān)閉。

廣告:

相關(guān)文章
asp.net中SqlDataReader使用時關(guān)閉數(shù)據(jù)庫連接的問題
cms新聞系統(tǒng)購買咨詢
掃描關(guān)注 廣州閱速軟件科技有限公司
掃描關(guān)注 廣州閱速科技