ExecuteReader如何工作 [英] How does ExecuteReader Work

查看:88
本文介绍了ExecuteReader如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果应用程序在服务器"ServerA"上运行,而数据库在另一台服务器"ServerB"上,则代码如下:

应用方法

Hi

If an application is running on a server "ServerA" and the database is on another server "ServerB", and the code is as below :

Method in application

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            if (UseFilePartValidate(reader))
            {
               AddPartsToFile(reader);
            }
        }
    }
}



将queryString参数设置为
的方法



Method called with queryString parameter set to

SELECT FileName, FilePart, FileID FROM SomeFileTable ORDER BY FilePartOrder



它在哪里过滤数据(ServerA或ServerB)?如果ServerA这样做意味着将所有数据拉到ServerA中,就像该表中所有文件的所有文件部分一样?

我认为这会将所有数据带到ServerA,但是最近我看到许多较旧的项目都可以像上面的示例一样工作,尤其是对于Collections而言,只是想可能我遗漏了一些东西.

在此先感谢



where will this do it''s filtering of the data, ServerA or ServerB? If ServerA does that mean it will pull all the data across to ServerA, as in all the file parts of all the files in that table?

I assume this will bring all the data across to ServerA but lately I''ve seen with a lot of older projects that it works as in the example above, especially with Collections and just thought maybe I was missing something.

Thanks in advance

推荐答案

由于您的命令不执行任何过滤,因此它们都不起作用!

如果您的命令是:
Since your command doesn''t do any filtering, none of them!

If your command was:
SELECT FileName, FilePart, FileID FROM SomeFileTable WHERE FileID < 20 ORDER BY FilePartOrder

然后,ServerB将执行过滤,因为它接收到整个命令.这就是为什么你不应该说的原因之一

Then ServerB will do the filtering, since it receives the whole command. That is one of the reasons why you shouldn''t say

SELECT * FROM myTable

,而不是指定字段如您的示例所示.这浪费了传输的带宽以及服务器上的内存-例如,当表中包含图像时,这可能很重要.

假设您使用的是SQL Server或MySQL:如果出于某些奇怪的原因使用Access,则必须在此处执行命令时将所有数据都拉到ServerA,以便在那里进行任何过滤.

instead of specifying the fields you want as in your example. It wastes bandwidth on the transfer as well as memory on the server - this can be significant when your table contains images for example.

This assumes you are using SQL Server, or MySQL: if you are using Access for some strange reason, then all the data has to be pulled to ServerA as the command is executed there so any filtering will be done there.


这篇关于ExecuteReader如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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