SQL 数据检索 - C#
我如何在循环或其他内容中从表中检索数据。这意味着我想一次逐行检索。但行的顺序可能不同。 例如,第一次我想要第 5 行,然后是第 2 行,然后是第 9 行……依此类推。
我通过互联网搜索到了它。我只得到两个答案。
使用多个 SqlConnection 对象。
reader= sqlCommand.ExecuteReader(); while(reader.Read()){ reader["列名"].ToString(); }
如果你遇到我的问题,请帮助 谢谢。
how do i retrieve data from a table within a loop or something. That means I want to retrieve row by row at a time. but the sequence of rows may differ.
For example, 1st time i want 5rd row,then 2nd, then 9...so on.
I searched it through the internet. I got only two answers.
Use several SqlConnection objects.
reader= sqlCommand.ExecuteReader();
While(reader.Read()){
reader["Column Name"].ToString();
}
If you got my problem, please help me
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来您应该更正数据层以按照要处理的顺序返回值。这将是最简单和最快的! :)
作为替代方案,我建议您将结果加载到数据表中:
/Alex
Sounds like you should correct your data layer to return the values in the order you are going to process them. It would be easiest and fastest! :)
As an alternative I'd suggest that you load your result into a DataTable:
/Alex
最好将数据读入数据集,如下例所示:
http://quickstart.developerfusion.co.uk/quickstart/howto/doc/adoplus/GetDataFromDB.aspx
It's probably best to read your data into a DataSet, as shown in this example:
http://quickstart.developerfusion.co.uk/quickstart/howto/doc/adoplus/GetDataFromDB.aspx
阅读器方法通常是可行的方法,但您的查询(或 SP)必须已经按照您想要检索的顺序选择数据。
或者,您可以将所有内容加载到数据集中,并对其中的行进行随机访问。
The reader approach is usually the way to go, but your query (or SP) must already select the data in the order you want to retrieve it.
Alternatively you can load everything into a DataSet and do random access on the rows in it.
我看到有两种方法:
1)获取一个sql语句中的所有行,然后访问内存中所需的行。
或者(“所有内容”太多,或者您需要最新数据)
2)仅获取您需要的特定行,然后再次获取下一行。
Two ways, that I see:
1) Get all rows in one sql statement, then access the row you need in memory.
Or (of "everything" is too much or you need recent data)
2) Get only the specific row you need, and again for the next row.
您究竟想要实现什么目标?从 DS 中检索随机行,或者您是否有特定的标准来选择要返回的行?如果是这样,您不能在将它们加载到阅读器之前订购它们吗?
What exactly are you trying to achieve? Retrieve random rows from a DS or do you have certain criteria for selecting which rows you want returned? And if so couldn't you order them before loading them into the reader?