获取组合框 SelectedIndex 数据并在 SELECT Query 中使用 - VB.net
Posted
技术标签:
【中文标题】获取组合框 SelectedIndex 数据并在 SELECT Query 中使用 - VB.net【英文标题】:Take combobox SelectedIndex data and use in SELECT Query - VB.net 【发布时间】:2015-09-30 07:55:23 【问题描述】:我有点绝望,所以我在这里!我对编程很陌生,并且被分配了一项任务,我需要使用一系列 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 个多小时(不是在开玩笑 - 我允许你笑)
【问题讨论】:
@Japz Divino 感谢您的回复。遗憾的是,我收到此代码的异常错误(这有助于生成 HTML 表)-该错误是针对 HtSample 行的。code
Private Function generateTable(ByVal lsData As List(Of Hashtable)) As String ' 生成表的开头 Dim sTable = "如果我对您的理解正确,您想在WHERE
子句中使用您的dropdown
选定项目。为此,请使用INNER JOIN
和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
【讨论】:
请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。如果只是猜测,还建议您不要发布答案。一个好的答案将有一个合理的理由说明它为什么可以解决 OP 的问题。 嘿,这个解决方案对我来说很有意义,也是我之前尝试过的 - 但我不确定如何让 SELECT 语句实际使用 selectedItem 值。这样做似乎不起作用。不过,感谢您的回应。 @SuperBiasedMan 帖子已修改。我的回答是基于我对问题的理解而非猜测。【参考方案2】:假设 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";
【讨论】:
【参考方案3】:请在 vb.net 中提供代码
这是我的代码,我想在class id
为条件的表格中显示奖金金额:
如果 class id
是 3,那么奖品会显示在我的名为 txtprize.text
的文本框中,而 class 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
【讨论】:
以上是关于获取组合框 SelectedIndex 数据并在 SELECT Query 中使用 - VB.net的主要内容,如果未能解决你的问题,请参考以下文章
如何将组合框中的 SelectedIndex 连接到列表连接字符串?
如何从我的SQL SERVER获取我的数据库列表,并使用javafx将其添加到组合框中