使用 SQLDataReader 有哪些优点/缺点?

发布于 2024-07-07 20:08:39 字数 62 浏览 4 评论 0原文

SqlDataReader 是处理存储过程的更快方法。 使用 SQLDataReader 有哪些优点/缺点?

SqlDataReader is a faster way to process the stored procedure. What are some of the advantage/disadvantages of using SQLDataReader?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朕就是辣么酷 2024-07-14 20:08:39

我假设你的意思是“而不是将结果加载到数据表中”?

优点:您可以控制数据的加载方式。 您可以请求特定的数据类型,并且除非您愿意,否则最终不会将整个数据集同时加载到内存中。 基本上,如果您想要数据但不需要数据表(例如,您将填充自己类型的集合),则您不会获得中间步骤的开销。

缺点:您可以控制数据的加载方式,这意味着更容易犯错误并且需要做更多工作。

您在这里的用例是什么? 您是否有充分的理由相信使用普通(或强类型)数据表的开销会严重损害性能? 如果我有充分的理由,我只会直接使用 SqlDataReader。

I assume you mean "instead of loading the results into a DataTable"?

Advantages: you're in control of how the data is loaded. You can ask for specific data types, and you don't end up loading the whole set of data into memory all at the same time unless you want to. Basically, if you want the data but don't need a data table (e.g. you're going to populate your own kind of collection) you don't get the overhead of the intermediate step.

Disadvantages: you're in control of how the data is loaded, which means it's easier to make a mistake and there's more work to do.

What's your use case here? Do you have a good reason to believe that the overhead of using a normal (or strongly typed) data table is significantly hurting performance? I'd only use SqlDataReader directly if I had a good reason to do so.

め可乐爱微笑 2024-07-14 20:08:39

关键优势显然是速度 - 这是您选择 SQLDataReader 的主要原因。

尚未提及的一个潜在缺点是 SQLDataReader 只能向前,因此您只能按顺序浏览一次记录 - 这是让它如此之快的原因之一。 在许多情况下,这没问题,但如果您需要多次迭代记录或添加/编辑/删除数据,则需要使用其中一种替代方法。

它还会保持连接状态,直到您处理完所有记录并关闭阅读器(当然,您可以选择提前关闭它,但随后您将无法访问任何剩余记录)。 如果您要在迭代记录时对记录执行任何冗长的处理,您可能会发现影响到数据库的其他连接。

The key advantage is obviously speed - that's the main reason you'd choose a SQLDataReader.

One potential disadvantage not already mentioned is that the SQLDataReader is forward only, so you can only go through the records once in sequence - that's one of the things that allows it to be so fast. In many cases that's fine but if you need to iterate over the records more than once or add/edit/delete data you'll need to use one of the alternatives.

It also remains connected until you've worked through all the records and close the reader (of course, you can opt to close it earlier, but then you can't access any of the remaining records). If you're going to perform any lengthy processing on the records as you iterate over them, you may find that you impact other connections to the database.

南渊 2024-07-14 20:08:39

这取决于您需要做什么。 如果您从数据库中获取一页结果(例如 20 条记录),最好使用数据适配器来填充 DataSet,并将其绑定到 UI 中的某些内容。

但如果您需要处理多条记录(一次处理 1 条记录),请使用 SqlDataReader。

It depends on what you need to do. If you get back a page of results from the database (say 20 records), it would be better to use a data adapter to fill a DataSet, and bind that to something in the UI.

But if you need to process many records, 1 at a time, use SqlDataReader.

纵性 2024-07-14 20:08:39

优点:速度更快,内存更少。

缺点:必须保持连接,必须记得关闭阅读器。

Advantages: Faster, less memory.

Disadvantages: Must remain connected, must remember to close the reader.

想你只要分分秒秒 2024-07-14 20:08:39

数据可能不是决定性的,你无法控制自己的行为,这就是为什么路上的送牛奶人总是必须随身携带数据,否则他们会被数据破解,而警察不会携带任何数据,因为他们认为保留他人的数据是错误的,这样做也是错误的。 有一个女孩住在谢菲尔德,她大部分时间都喜欢出去玩,这就是为什么我不喜欢和她说话,因为她的父母和她的其他朋友被带到了和平花园,那是一个地方每个人都喜欢唱歌并留下来。 通常著名的名人都会在那里闲逛,但那里总是有最高的安全措施,因为我们不想被困在他们的末端。 KK 现在见你,我需要 2 个人去西区平静一下!£"$$$ 由金钱组成的 MAN$$$$

The data might not be concluesive and you are not in control of your actions that why the milk man down the road has always got to carry data with him or else they gona get cracked by the data and the policeman will not carry any data because they think that is wrong to keep other people's data and its wrong to do so. There is a girl who lives in Sheffield and she loves to go out and play most the times that she s in the house that is why I dont like to talk to her because her parents and her other fwends got taken to peace gardens thats a place that everyone likes to sing and stay. usually famous Celebs get to hang aroun dthere but there are always top security because we dont want to get skanked down them ends. KK see u now I need 2 go and chill in the west end PEACE!!!£"$$$ Made of MOney MAN$$$$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文