0

我有点绝望,所以我在这里!我对编程很陌生,并且被分配了一项任务,我需要使用一系列 SQL 查询来生成一个简单的 HTML 报告表。还有一个用户输入,他们从组合框中选择 ClinicID 并单击按钮以生成报告。

基本上,我有一个用“ClinicID”填充的组合框,如下所示。我还确保 SelectedIndex 正常工作。我需要在下面提供的 SQL 查询方法中以某种方式使用它。

Public Class frmReport1
'Set lsData for Clinics table
Dim lsData As List(Of Hashtable)


'On form load
Private Sub frmReport1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    cboClinicID.DropDownStyle = ComboBoxStyle.DropDownList

    'Instantiate new ClinicController object
    Dim cController As ClinicController = New ClinicController

    'Load ClinicID
    lsData = cController.findId()

    For Each clinic In lsData
        cboClinicID.Items.Add(CStr(clinic("ClinicID")))
    Next

End Sub

'Selected Index 
Private Sub cboClinicID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClinicID.SelectedIndexChanged
    Dim selectedIndex As Integer = cboClinicID.SelectedIndex
    Dim selectedItem As Object = cboClinicID.SelectedItem

    'Print in debug window
    Debug.Print("Selected clinicID: " & selectedItem.ToString())
    Debug.Print("Selected clinicID index: " & selectedIndex.ToString())

    Dim htData = lsData.Item(selectedIndex)


End Sub

SQL 查询方法 - **注意,我从两个不同的表中提取

“?”在哪里?是,我认为我必须在“SelectedItem”中工作,但我不知道怎么做!

期望的结果:用这三个选定字段输出的 html 表格。

Public Class ClinicOrderController

Public Const CONNECTION_STRING As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PharmDB.accdb"

'Dim cController As ClinicController = New ClinicController
'Dim oController As OrderController = New OrderController

Public Function findClinicOrder() As List(Of Hashtable)

    'Instantiates a connection object
    Dim oConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
    'Instantiates a list of hashtables
    Dim lsData As New List(Of Hashtable)

    Try
        Debug.Print("Connection string: " & oConnection.ConnectionString)

        oConnection.Open()
        Dim oCommand As OleDbCommand = New OleDbCommand
        oCommand.Connection = oConnection

        'Stored in the CommandText property of the command object
        'SELECT SQL statement
        oCommand.CommandText = "SELECT clinics.clinic_id, orders.date_ordered, orders.total_price FROM clinics, orders WHERE clinics.clinic_id = orders.clinic_id AND clinics.clinic_id = ? ORDER BY clinics.clinic_id"

        'Compiles the prepared statement
        'oCommand.Prepare()
        'Executes the SQL statement and stores the results in data reader object
        Dim oDataReader = oCommand.ExecuteReader()

        'Process data set in Hashtable
        Dim htTempData As Hashtable
        Do While oDataReader.Read() = True
            htTempData = New Hashtable
            htTempData("ClinicID") = CStr(oDataReader("clinic_id"))
            htTempData("DateOrdered") = CStr(oDataReader("date_ordered"))
            htTempData("OrderTotalPrice") = CStr(oDataReader("total_price"))
            lsData.Add(htTempData)

        Loop

        Debug.Print("The record was found.")

    Catch ex As Exception
        Debug.Print("ERROR:" & ex.Message)
        MsgBox("An error occured!")
    Finally
        oConnection.Close()
    End Try

    'Return list of hashtables to the calling function
    Return lsData

End Function

真的,真的很感谢这里的任何帮助。我已经为此苦苦挣扎了 8 个多小时(不是在开玩笑——我允许你笑)

4

3 回答 3

0

假设 Clinic_id 是数据库中的数字字段:(否则只需用单引号 ('') 将其括起来)

string clinicID = cboClinicID.SelectedItem.ToString();
string sql = string.Format(@"SELECT clinics.clinic_id, orders.date_ordered, orders.total_price
                             FROM clinics, orders
                             WHERE clinics.clinic_id = orders.clinic_id
                             AND clinics.clinic_id = {0}
                             ORDER BY clinics.clinic_id", clinicID);

oCommand.CommandText = sql;

你也可以这样做:

string sql = "SELECT clinics.clinic_id, orders.date_ordered, orders.total_price " +
             "FROM clinics, orders " +
             "WHERE clinics.clinic_id = orders.clinic_id " +
             "AND clinics.clinic_id = " + clinicID + " " +
             "ORDER BY clinics.clinic_id";
于 2015-09-30T22:34:25.277 回答
0

如果我理解正确,您想在您的条款中使用您dropdown选择的项目。WHERE为此,请使用INNER JOINwith修改您的加入,ON然后将您的过滤置于WHERE条件中。希望下面的代码会有所帮助。

    SELECT clinics.clinic_id,
      , orders.date_ordered
      , orders.total_price 
    FROM clinics INNER JOIN orders ON clinics.clinic_id = orders.clinic_id
    WHERE clinics.clinic_id = selectedItem.ToString()
    ORDER BY clinics.clinic_id

如果 selectedItem.ToString() 不起作用,您可以尝试SelectedValue

于 2015-09-30T08:05:35.847 回答
-1

请在 vb.net 中提供代码

这是我的代码,我想从class id条件表中显示奖金金额:

如果class id是 3,那么奖品将显示在我命名的文本框中txtprize.textclass Id显示在列表框中。

Private Sub listClassID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listClassID.SelectedIndexChanged
Dim classIdlist As String
classIdlist = New String(listClassID.SelectedItem.ToString)
Dim strSQL As String = "select [Prize Amount] from Master_Class WHERE [Class ID]  =" & classIdlist
Dim dr As SqlDataReader

Try
    con.Open()
    cmd = New SqlCommand(strSQL, con)
    dr = cmd.ExecuteReader()
    If dr.Item(0) Then
        txtPrize.Text = dr("[Prize Amount]").ToString
    End If

    dr.Close()
    cmd.Dispose()
    con.Close()

Catch ex As Exception
    MsgBox(ex.Message)
    con.Close()
End Try
End Sub
于 2017-08-29T08:45:59.487 回答