问题
Here is a sample code of using the SqlDataReader
:
// Working with SQLServer and C#
// Retrieve all rows
cmd.CommandText = "SELECT some_field FROM data";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
EDIT :
I mean I want to understand whether there is the same mechanism when retrieving data from database in a while-loop (in case of SqlDataReader
) as it does when working with the SQLite.
// working with SQLite and Java
if (cursor.moveToFirst()) {
do {
String data = cursor.getString(cursor.getColumnIndex("data"));
// do what ever you want here
} while(cursor.moveToNext());
}
cursor.close();
回答1:
No, there's no cursor on the server side unless your command is calling a stored procedure that uses a cursor. The server is returning a plain-vanilla SQL result set, one row at a time, when you use a SqlDataReader. Obviously, the data's got to be somewhere before you can read it, and that place would be buffer(s) that SQL Server and the drivers manage.
If you were to push this into using something like a Dataset, then all the rows would be in RAM at once.
回答2:
While you use cursor which returns many dbsets you can obtain many result sets like that:
while (oSqlDataReader.HasRows)
{
while (oSqlDataReader.Read())
{
// oSqlDataReader.Getdata...
}
oSqlDataReader.NextResult();
}
来源:https://stackoverflow.com/questions/43452148/does-a-net-sqldatareader-object-use-a-database-cursor-or-the-whole-result-set