0

我将如何从“DAILY_CALCULATIONS”表中检索数据

  Dim SqlQuery As String = "SELECT WEEKLY_TIMECARD.*,DAILY_CALCULATIONS.*,EMPLOYEE_PROFILES.EMPLOYEE_NUMBER "
    SqlQuery = SqlQuery + " FROM WEEKLY_TIMECARD, DAILY_CALCULATIONS, employee_profiles WHERE employee_profiles.employee_number"
    SqlQuery = SqlQuery + " = WEEKLY_TIMECARD.employee_number AND EMPLOYEE_PROFILES.EMPLOYEE_NUMBER = DAILY_CALCULATIONS.EMPLOYEE_NUMBER AND"
    SqlQuery = SqlQuery + " WEEKLY_TIMECARD.WEEK_NUMBER = DAILY_CALCULATIONS.WEEK_NUMBER AND WEEKLY_TIMECARD.EMPLOYEE_NUMBER ="
    SqlQuery = SqlQuery + "'" + txtENumber.Text + "' AND WEEKLY_TIMECARD.WEEK_NUMBER = '" + CBWeekNum.SelectedItem + "'"

    Try

        con.Open()

        If con.State = ConnectionState.Open Then
            con.ChangeDatabase("shopclocksoftware")
            Command = New MySqlCommand(SqlQuery, con)
            Data = Command.ExecuteReader()

            While Data.Read
                txtHUnits.Text = Data(2).ToString
                txtOUnits.Text = Data(3).ToString
            End While

我查看了 Data.NextResult 方法,但我无法正确实现它,有什么想法吗?

4

1 回答 1

0
Dim SqlQuery As String = "SELECT wtc.A, wtc.B, wtc.C, dc.D, dc.E, ep.F, ep.G"
SqlQuery = SqlQuery + " FROM WEEKLY_TIMECARD wtc, DAILY_CALCULATIONS dc, employee_profiles ep "
SqlQuery = SqlQuery + " WHERE ep.employee_number"
SqlQuery = SqlQuery + " = wtc.employee_number AND ep.EMPLOYEE_NUMBER = dc.EMPLOYEE_NUMBER AND"
SqlQuery = SqlQuery + " wtc.WEEK_NUMBER = dc.WEEK_NUMBER AND wtc.EMPLOYEE_NUMBER ="
SqlQuery = SqlQuery + "@ENumber AND wtc.WEEK_NUMBER = @CBWeekNum"

Try
    con.Open()

    If con.State = ConnectionState.Open Then
        con.ChangeDatabase("shopclocksoftware")
        Command = New MySqlCommand(SqlQuery, con)
        Command.Parameters.AddWithValue("@ENumber", txtENumber.Text);
        Command.Parameters.AddWithValue("@CBWeekNum", CBWeekNum.SelectedItem);

        Data = Command.ExecuteReader()

        Dim aOrdinal As Integer = reader.GetOrdinal("a")
        Dim bOrdinal As Integer = reader.GetOrdinal("b")

        While Data.Read
            txtHUnits.Text = Data.GetString(aOrdinal)
            txtOUnits.Text = Data.GetString(bOrdinal)
        End While
....
Exit Try

需要注意的几点:

  1. 我将您的 SQL 更改为使用别名,并返回明确的字段名称。using*通常是一个坏主意,尤其是当您实际上只需要列的子集时。
  2. 查询现在已参数化,因此您不会受到 SQL 注入攻击
  3. 我利用GetOrdinal来定位结果集中任何列的位置。这是一个小的优化。
  4. GetString如果您返回的列是字符串,则调用。否则,您可以像以前一样调用。

显然我现在不知道你的列名,所以 SQL 不起作用,但你应该明白了。

于 2013-02-07T17:29:12.407 回答