随机抽取一些数据并总结相应行的值

时间:2014-02-01 14:41:01

标签: database vb.net ms-access-2010

我有一个看起来像这样的数据库

+----------+--------+-----------+
| WorkerID | TaskNo | TimeTaken |
+----------+--------+-----------+
| 111      | 1      | 7.5       |
+----------+--------+-----------+
| 114      | 1      | 2.5       |
+----------+--------+-----------+
| 111      | 2      | 3.5       |
+----------+--------+-----------+
| 112      | 2      | 1.5       |
+----------+--------+-----------+
| 111      | 3      | 2.5       |
+----------+--------+-----------+
| 112      | 3      | 4.5       |
+----------+--------+-----------+
| 113      | 3      | 3.5       |
+----------+--------+-----------+
| 112      | 4      | 3.5       |
+----------+--------+-----------+

- 列表继续 -

在很多帮助下,我终于可以在vb上显示它了

+--------+------------+------------------+
| TaskNo | NumWorkers | WorkersAvailable |
+--------+------------+------------------+
| 1      | 2          | 111, 114         |
+--------+------------+------------------+
| 2      | 2          | 111, 112         |
+--------+------------+------------------+
| 3      | 3          | 111, 112, 113    |
+--------+------------+------------------+
| 4      | 1          | 112              |
+--------+------------+------------------+

让我说我能够从每一行产生1个随机数,

  • 第1 - 114行
  • 第2行 - 111
  • 第3 - 111行
  • 第4行 - 112

它将以字符串格式显示,并以逗号分隔。

114,111,111,112

现在的问题是我如何知道同一行中的相应值。例如

114,111,111,112 =

  

2.5 + 3.5 + 2.5 + 3.5 = 12

我如何显示12?任何帮助将不胜感激

我的代码

Sub RefreshLv()

        Using conn = New OleDbConnection
            conn.ConnectionString = myConString
            conn.Open()

            Dim dt As New System.Data.DataTable("ListInfo")
            Using da

                ' fill the DataTable with three columns, the third column being a placeholder that we will fill in below
                Dim Sql As String = ("SELECT [TaskNo], COUNT(*) AS NumWorkers, '' AS WorkersAvailable " & "FROM ScheduleInfo GROUP BY [TaskNo]")
                da.SelectCommand = New OleDbCommand(Sql, conn)
                da.Fill(dt)

                Using cmd2 = New OleDbCommand()

                    ' create a Prepared Statement that we will use for each iteration
                    cmd2.Connection = conn
                    cmd2.CommandText = "SELECT [WorkerID] FROM ScheduleInfo " & "WHERE [TaskNo] = ? " & "ORDER BY [WorkerID]"
                    cmd2.Parameters.Add("?", OleDbType.Integer)
                    cmd2.Prepare()

                    LstViewScheduleInfo.Columns.Add("TaskNo", 150, HorizontalAlignment.Center)
                    LstViewScheduleInfo.Columns.Add("No of Workers", 150, HorizontalAlignment.Center)
                    LstViewScheduleInfo.Columns.Add("Workers Available", 150, HorizontalAlignment.Center)

                    ' foreach row of the DataTable, build the string of WorkerID values
                    Dim RandomEmployee As New List(Of Integer)
                    Dim rnd As New Random

                    For Each dr As System.Data.DataRow In dt.Rows

                        Dim ListEmployee As New List(Of Integer)
                        Dim workerList As String = ""
                        cmd2.Parameters(0).Value = dr("TaskNo")
                        Using rdr As OleDbDataReader = cmd2.ExecuteReader()

                            While rdr.Read()
                                ListEmployee.Add(rdr("WorkerID"))
                                workerList += ", " & rdr("WorkerID")
                            End While
                        End Using

                        ' remove leading ", "
                        'Dim Qpass As String
                        dr("WorkersAvailable") = workerList.Substring(2)
                        Dim randomvalue As Integer = ListEmployee(rnd.Next(0, 2))
                        'Qpass = randomvalue.ToString()
                        'MessageBox.Show(Qpass)
                        RandomEmployee.Add(randomvalue)
                    Next
                    Dim zxc = String.Join((","), RandomEmployee.ToArray())
                    LblRandom1.Text = zxc

                End Using

                ' for demo purposes, just dump the DataTable to the console
                For Each dr As DataRow In dt.Rows()
                    Dim lst As ListViewItem
                    lst = LstViewScheduleInfo.Items.Add(dr(0))

                    For i As Integer = 1 To dt.Columns.Count - 1
                        lst.SubItems.Add(dr(i))
                    Next
                Next

            End Using
            conn.Close()
        End Using


    End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        RefreshLv()

 End Sub
    Private Sub BtnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRandom.Click

        LstViewScheduleInfo.Clear()
        RefreshLv()

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您已经遍历DataTable中的Rows,为每个[TaskNo]提取[WorkerID]值。您可以简单地同时检索相应的[TimeTaken]值。也就是说,而不是

cmd2.CommandText = "SELECT [WorkerID] FROM ScheduleInfo " & "WHERE [TaskNo] = ? " & "ORDER BY [WorkerID]"

你可以做到

cmd2.CommandText = "SELECT [WorkerID], [TimeTaken] FROM ScheduleInfo " & "WHERE [TaskNo] = ? " & "ORDER BY [WorkerID]"

然后,除了为每个[TaskNo]随机选择[WorkerID]值并将其添加到List之外,您还可以将相应的[TimeTaken]值添加到另一个List。

运行完所有[TaskNo]值后,您将获得随机选择的[WorkerID]值列表及其相应的[TimeTaken]值。您可以通过在列表中添加[TimeTaken]值来获得总时间。