我有點絕望,因此我爲什麼在這裏!我對編程相當陌生,並且已經給了一個任務,我需要使用一系列SQL查詢來生成一個簡單的HTML報表。還有一個用戶輸入,他們從組合框中選擇ClinicID並單擊一個按鈕來生成報告。採取組合框SelectedIndex數據和使用SELECT查詢 - VB.net
基本上,我有一個組合框,我已經填充了'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功能generateTable(BYVAL lsData方式列表(哈希表中))作爲字符串 '生成表 昏暗穩定=開始 「<表邊界=」 「1」 「>」 &vbCrLf 昏暗htSample作爲Hashtable = lsData。項(0) 昏暗lsKeys方式列表(字符串)=新的列表(串) lsKeys.Add( 「ClinicID」) lsKeys.Add( 「DateOrdered」) lsKeys.Add( 「OrderTotalPrice」) '代碼' – antisonfire
這不是什麼關於它(即詢問可能對未來讀者有幫助的具體問題)。爲了分析代碼,你應該去Code Review(http://codereview.stackexchange.com/)。無論如何,不清楚和期望人們浪費時間去理解和解決你的問題似乎並不是獲得幫助的最佳途徑。 – varocarbas
這篇文章屬於Code Review。 – varocarbas