See the following questions on the 9CBS forum, if DataReader is used as DataSource, after DataGrid.Database, in the following two cases, the value of DataReader isclosed is different.
1.idaReader reader = somedbcommand.executeReader (); DataGrid1.datasource = Reader; DataGrid1.DATABIND ();
Here, the value of Reader isclosed is False
2.idaReader reader = Somedbcommand.executeReader (Commandbehavior.CloseConnection); DataGrid1.datasource = Reader; DataGrid1.DATABIND ();
Here, the value of reader isclosed is True.
The conclusion is that if DataReader's Commandbehavior is a CloseConnection, DataGrid1.DATABIND () calls DataReader's Close method in some (perhaps non-direct) mode. I found a similar discussion on the Internet, but they did not give the reason.
When DataReader is used as a DataSource, DataReader is treated as Ienumerable, from IEnumerable to get the IEnumerator, then you can use its MoveNext / Current to get each record in turn, is MoveNext call DataReader's Close method? A test
Using system; using system.data.oledb; using system.data.sqlclient; using system.collections;
class TestClose {static void Main () {SqlConnection conn = new SqlConnection ( "server = localhost; database = pubs; uid = sa; pwd =;"); SqlCommand cmd = new SqlCommand ( "select * from authors", conn);
Conn.open (); sqldataareader reader = cmd.executeReader (Commandbehavior.CloseConne); TestRead (Reader); Reader.close ();
Conn.open (); Reader = cmd.ExecuteReader (Commandbehavior.CloseConne); TestMoveNext (Reader); Reader.Close ();
}
Static void testread {while (reader.read ()); console.writeline ("After Read (), Reader Is Closed? {0}", reader.isclosed;
Static void Testmovenext (iDataReader Reader) {IEnumerator E = ((IEnumerable) Reader .geTenumerator (); while (E.MOVENEXT ()); console.writeline ("After MoveNext (), Reader is closed? {0}", Reader.isclosed;}}
Output is after read (), Reader is closed? Falseafter MoveNext (), Reader IS Closed? True
Sure enough, use the Lutz Roeder's Reflector (strongly recommended!) Tool to see the implementation of SqlDataReader in Net.
IEnumerator system.collections.ienumerable.GeteNumerator () {return new dbenumerator (this, (commandbehavior.default! = (Commandbehavior.closeconnection & this._behavior));}
It can be seen that the Commandbehavior of SqlDataReader is indeed passing, and then see the implementation of DBENUMERATOR.
Public DBENUMERATOR (iDataReader Reader, Bool ClosReader) {if (Reader == Null) {throw adp.argumentnull ("Reader");} this._reader = reader; this.closeader = closeader;}
Public Bool MoveNext () {Object [] objarray1; if (this._schemafo == null) {this.buildschemainfo ();} this._current = null; if (this._reader.read ()) {Objarray1 = new Object [ this._schemaInfo.Length]; this._reader.GetValues (objArray1); this._current = new DbDataRecord (this._schemaInfo, objArray1, this._descriptors, this._fieldNameLookup); return true;} if (this.closeReader) {this ._reader.close ();} Return False;
Sure enough, if DataReader's Commandbehavior is a commandbehavior.closeConnection, MoveNext calls a Close method!