I have a question regarding the DataReaders.
I think I am missing a concept somewhere and would welcome any help.
In all of the sources I have consulted the DataReader is broadly described as
'a tool to perform read only, forward only against a data source which only reads one row at a time. It does not hold a big dataset in memory.'
So I coded the simple example below but found that when I loop through the DataReader results, the results do not
reflect any changes another program makes to a row that does not yet have the focus in the while loop.
In other words, disproving the 'one row at a time' and 'no big dataset in memory' advantages.
In other words, if I:-
1) break in debug on the 'End While' after the first .READ
2) change a field value in the second row in the table that the .READ has yet to read
3) relaese the debug break to run to the 'End While' again
4) check the contents of row 2 in the debug session and find the change I made outside the program is not reflected
in row 2 in the program
To me this means the results are a dataset behind the scenes at the time the DataReader was executed.
Therefore is must be holding the data in memory and merely allows me to step through the results with a .READ.
So where is the big advantage of using it over a DataAdapter that also reads all of the data? The only difference
the functionality is missing on a DataReader to perform updates which I suppose can be more efficient when performing read only actions.
Dazed and confused.
Remember.. 'Depression is just anger without enthusiasum'.
I think I am missing a concept somewhere and would welcome any help.
In all of the sources I have consulted the DataReader is broadly described as
'a tool to perform read only, forward only against a data source which only reads one row at a time. It does not hold a big dataset in memory.'
So I coded the simple example below but found that when I loop through the DataReader results, the results do not
reflect any changes another program makes to a row that does not yet have the focus in the while loop.
In other words, disproving the 'one row at a time' and 'no big dataset in memory' advantages.
In other words, if I:-
1) break in debug on the 'End While' after the first .READ
2) change a field value in the second row in the table that the .READ has yet to read
3) relaese the debug break to run to the 'End While' again
4) check the contents of row 2 in the debug session and find the change I made outside the program is not reflected
in row 2 in the program
To me this means the results are a dataset behind the scenes at the time the DataReader was executed.
Therefore is must be holding the data in memory and merely allows me to step through the results with a .READ.
So where is the big advantage of using it over a DataAdapter that also reads all of the data? The only difference
the functionality is missing on a DataReader to perform updates which I suppose can be more efficient when performing read only actions.
Code:
Dim MyString As String
Dim MySQLCommand As New System.Data.SqlClient.SqlCommand
Dim MySQLConnection As New System.Data.SqlClient.SqlConnection
Try
MySQLConnection.ConnectionString = "Integrated Security=true;Initial Catalog=MyDatabase;server=MyServer;"
MySQLConnection.Open()
MySQLCommand.Connection = MySQLConnection
MySQLCommand.CommandType = CommandType.Text
MySQLCommand.CommandText = "SELECT * FROM MYTABLE"
Dim mySQLReader As System.Data.SqlClient.SqlDataReader
mySQLReader = MySQLCommand.ExecuteReader()
While mySQLReader.Read
MyString = "Field 0 : " + mySQLReader.Item(0).ToString + " Field 1 : " + mySQLReader.Item(1).ToString
Console.WriteLine(MyString)
End While
mySQLReader.Close()
MySQLConnection.Close()
Catch ex As Exception
MessageBox.Show("Unexpected Program Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Dazed and confused.
Remember.. 'Depression is just anger without enthusiasum'.