Does a .NET SqlDataReader object use a database cursor, or the whole result set is loaded into RAM?

流过昼夜 提交于 2021-02-18 22:59:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!