SQL 数据检索 - C#

发布于 2024-10-08 15:18:03 字数 305 浏览 4 评论 0原文

我如何在循环或其他内容中从表中检索数据。这意味着我想一次逐行检索。但行的顺序可能不同。 例如,第一次我想要第 5 行,然后是第 2 行,然后是第 9 行……依此类推。

我通过互联网搜索到了它。我只得到两个答案。

  1. 使用多个 SqlConnection 对象。

  2. 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.

  1. Use several SqlConnection objects.

  2. reader= sqlCommand.ExecuteReader();
    While(reader.Read()){
    reader["Column Name"].ToString();
    }

If you got my problem, please help me
Thank you.

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

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

发布评论

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

评论(5

动听の歌 2024-10-15 15:18:03

听起来您应该更正数据层以按照要处理的顺序返回值。这将是最简单和最快的! :)

作为替代方案,我建议您将结果加载到数据表中:

    DataTable table = new DataTable();
    using ( SqlCommand command = new SqlCommand() )
    {
           // TODO: Set up your command here
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            adapter.Fill(table);
        }
    }

    // Use your DataTable like this...

    if ( table.Rows.Count >= 5 ) {
        DataRow firstRow = table.Rows[0]; // #1 row
        DataRow fifthRow = table.Rows[4]; // #5 row
        DataRow secondRow = table.Rows[1]; // #2 row
    }

/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:

    DataTable table = new DataTable();
    using ( SqlCommand command = new SqlCommand() )
    {
           // TODO: Set up your command here
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            adapter.Fill(table);
        }
    }

    // Use your DataTable like this...

    if ( table.Rows.Count >= 5 ) {
        DataRow firstRow = table.Rows[0]; // #1 row
        DataRow fifthRow = table.Rows[4]; // #5 row
        DataRow secondRow = table.Rows[1]; // #2 row
    }

/Alex

南汐寒笙箫 2024-10-15 15:18:03

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

↘紸啶 2024-10-15 15:18:03

阅读器方法通常是可行的方法,但您的查询(或 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.

无力看清 2024-10-15 15:18:03

我看到有两种方法:

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.

金兰素衣 2024-10-15 15:18:03

您究竟想要实现什么目标?从 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?

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