【问题标题】:'For each' loop based in SQL select statement基于 SQL 选择语句的“For each”循环
【发布时间】:2014-10-18 13:00:09
【问题描述】:

我试图弄清楚如何为 SQL 查询返回的每个不同值执行“For Each”循环。

这是我的伪代码。

connection.ConnectionString = "server=***01\SQLEXPRESS; database=Billing; integrated security=yes"
command.Connection = connection
command.CommandType = CommandType.Text
command.CommandText = "SELECT DISTINCT [Customer] FROM [Billing]

For Each... Distinct value returned above

    command.CommandType = CommandType2.Text
    command.CommandText2 = "Select * FROM [Billing] WHERE [Customer] = [DISTINCT VALUE FROM ABOVE]
    dataAdapter.SelectCommand = command

        'Fill data to datatable
        connection.Open()
        dataAdapter.Fill(datatableMain)
        connection.Close()

Then Export (I am ok with the Export code)

本质上,我需要能够循环,直到我为每个客户导出了一个数据表。

希望这是有道理的,非常感谢任何帮助。

【问题讨论】:

  • 那是真实的代码吗?为什么您要循环每个不同的客户只是为了能够为这些客户选择所有比林斯?首先选择所有账单不是更有效吗? Select * FROM [Billing] ORDER BY Customer
  • 您可以在 Billing 表和 Group By Customer 上使用自联接。
  • @PradeepKumar:假设他不想要每个客户的所有行。内部选择反对。
  • 我需要为每个客户提供单独的输出。所以对于客户 A,我想导出一个包含客户 A 的所有账单明细的文件,对于客户 B,我想导出所有客户 B 的账单明细等等......
  • ok.. 在这种情况下,您只需填写一次数据表 (SELECT * FROM Billing ORDER BY Customer)。然后,您可以过滤每个客户并获取与该客户相关的行。这比分别为每个客户循环和填充数据要高效得多。

标签: sql vb.net for-loop foreach


【解决方案1】:

这是一些未经测试的代码。但它会让你对我在说什么有一个公平的认识(在问题的 cmets 中)。

Sub Whatever()
    connection.ConnectionString = "server=***01\SQLEXPRESS; database=Billing; integrated security=yes"
    connection.Open()
    Using da As New SqlDataAdapter("Select * FROM [Billing] ORDER BY Customer", connection)
        da.Fill(datatableMain)
    End Using
    connection.Close()

    ' get distinct customers
    Dim dv As New DataView(datatableMain)
    Dim distinctCustomers As DataTable = dv.ToTable(True, "Customer")

    For Each customer As DataRow In distinctCustomers.Rows
        ' this messagebox is only to give you an idea which customer you are printing
        ' not required in actual code.
        MessageBox.Show("Exporting Customer... " & customer("Customer").ToString)

        Dim customerRows() As DataRow = datatableMain.Select("Customer=" & customer("Customer").ToString)  '<-- put single quotes around value if "Customer" field is of string type. e.g. "Customer='value'"
        For Each customerRow As DataRow In customerRows
            ' all the rows related to this customer are here
            ' do whatever you do to export

        Next
    Next
End Sub

【讨论】:

  • 谢谢@Pradeep,看起来不错。但是它目前错误。附加信息:找不到列 [ALL001]。 ALL001 我的第一个客户值,不是列名?谢谢
  • 我的字段名为“CustomerLookup”。这是引发错误的行: Dim customerRows() As DataRow = datatableMain.Select("CustomerLookup=" & customer("CustomerLookup").ToString)
  • 如果在值周围加上单引号会发生什么。 Dim customerRows() As DataRow = datatableMain.Select("CustomerLookup = '" &amp; customer("CustomerLookup").ToString &amp; "'")
  • 感谢@Pradeep 似乎已经成功了。我会将这篇文章标记为已回答。不幸的是,我的代码现在挂了。我打开了一个新线程并在此处发布了我的最新代码。 stackoverflow.com/questions/25488938/…谢谢
猜你喜欢
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多