.NET SqlDataReader对象是否使用数据库游标,或者整个结果集都已加载到RAM中? [英] Does a .NET SqlDataReader object use a database cursor, or the whole result set is loaded into RAM?

查看:35
本文介绍了.NET SqlDataReader对象是否使用数据库游标,或者整个结果集都已加载到RAM中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是使用 SqlDataReader 的示例代码:

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));
    }
}

我的意思是我想了解在while循环中(如果使用 SqlDataReader )从数据库中检索数据时是否具有与使用SQLite时相同的机制.

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();

推荐答案

否,除非您的命令正在调用使用游标的存储过程,否则服务器端没有游标.当您使用SqlDataReader时,服务器将返回普通的SQL结果集,一次返回一行.显然,数据必须先放在某个地方才能读取,并且该地方应该是SQL Server和驱动程序管理的缓冲区.

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.

如果要使用诸如数据集之类的方法将其强制使用,则然后所有行都将同时存储在RAM中.

If you were to push this into using something like a Dataset, then all the rows would be in RAM at once.

这篇关于.NET SqlDataReader对象是否使用数据库游标,或者整个结果集都已加载到RAM中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆