显示来自 SQL 数据库的特定数据,没有要过滤的列。 (VB.NET)

Displaying specific Data from SQL Database without a Column to filter by. (VB.NET)

我有一个 DataGridView,它使用 tblAttendanceTimesTableAdapter.GetDataBy(intEmployee)

dbo.tblAttendanceTimes 显示 Data

很明显,它将显示所选员工的所有 Data

员工分为 3 组。我想做的是在 dbo.tblAttendanceTimes 中的 [intApprovedOvertime] Column 中为我正在查看的特定组显示值为 0 的所有记录。

只是 GetDataBy(intEmployee) 变得困难,Table 中没有 [intAttendanceGroup] Column 或者我可以 GetDataBy(intAttendanceGroup)

目前我拥有组中员工的所有 pk 值,但我正在努力检索该员工的所有记录显示它们然后执行下一个员工并添加到之前显示的。

希望我说的有道理,如需任何其他信息,请询问。

两个解决方案可能很有趣:

  • 将数据库结果直接链接到 DGV 非常有利于轻松更新,但如果需要则移动行的次数较少
  • 将查询中的数据作为行插入,您首先需要定义 DGV 的列 DataGridView2.Columns.Add()

1/

Using con As New SqlClient.SqlConnection("YOUR SQL CONNECTION STRING")
    Dim sqlString As String = "SELECT * FROM Table WHERE intApprovedOvertime = 0 AND fkEmployee = pkEmployee"
    con.Open()
    Dim da As New SqlClient.SqlDataAdapter(sqlString, con)
    Dim ds As New DataSet
    da.Fill(ds, "Table")
    DataGridView1.DataSource = ds.Tables("Table")
    ds.Dispose()
    da.Dispose()
    SqlConnection.ClearPool(con)
End Using

2/

Using con As New SqlClient.SqlConnection("YOUR SQL CONNECTION STRING")
 Dim command As New SqlCommand("SELECT * FROM Table WHERE intApprovedOvertime = 0 AND fkEmployee = pkEmployee", con)
        Dim reader As SqlDataReader = command.ExecuteReader()
        While reader.Read()
            DataGridView1.Columns.Rows.Add(reader(0), reader(1))
        End While
End Using

在数字 2 中,您需要记住调整 reader(x) 以适应您需要添加到行中的值的数量。您还可以通过以下方式获取特定列:

"SELECT Table.Name, Table.Position FROM Table WHERE intApprovedOvertime = 0 AND fkEmployee = pkEmployee"

可能会有一些小的语法错误我没有IDE哈哈。