0
我正在vb.net中創建一個程序,並想知道是否有一種方法來執行多個查詢與一個按鈕,而無需添加第二個按鈕。vb.net搜索按鈕多個查詢
像我有兩個文本框,一個是日期,另一個是名稱。我想要做的是,如果我點擊搜索,並且日期和名稱框都填滿了,我希望它根據該日期搜索名稱。但如果只填寫名字,我想在這個名字下搜索所有內容。
這就是我所擁有的。現在它只做一個搜索:
Private Sub cmdSearch_Click(sender As System.Object, e As System.EventArgs) Handles cmdSearch.Click
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name " _
& ", start.timestamp `Time In` " _
& ", end.timestamp `Time Out` " _
& ", timediff(end.timestamp, start.timestamp) Duration " _
& "FROM user u " _
& ", user_group ug " _
& ", (" _
& "select * " _
& ", (" _
& "select event_id " _
& "from event L2 " _
& "where L2.timestamp > L1.timestamp " _
& "and L2.user_bannerid = ?bannerID " _
& "and L1.user_bannerid = ?bannerID " _
& "order by timestamp limit 1 " _
& ") stop_id " _
& "From event L1 " _
& ") start " _
& "join event end on end.event_id = start.stop_id " _
& "where start.status = 'In' " _
& "and end.status='Out' " _
& "and u.user_bannerid = ?bannerID " _
& "and start.user_bannerid = ?bannerID " _
& "and ug.user_bannerid = ?bannerID " _
& "and ug.group_id = ?GroupID " _
& " and start.group_id = ?GroupID " _
& "UNION " _
& "SELECT null, null, null, CAST(sum(duration) as Time) " _
& "FROM " _
& "(" _
& "SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name " _
& ", start.timestamp `Time In` " _
& ", end.timestamp `Time Out` " _
& ", timediff(end.timestamp, start.timestamp) duration " _
& "from user u " _
& ", user_group ug " _
& ", (" _
& "select * " _
& ", (" _
& "select event_id " _
& "from event L2 " _
& "where L2.timestamp > L1.timestamp " _
& "and L2.user_bannerid = L1.user_bannerid " _
& "order by timestamp " _
& "limit 1 " _
& ") stop_id " _
& "from event L1 " _
& ") start " _
& "join event end on end.event_id = start.stop_id " _
& "where start.status = 'In' " _
& "and end.status = 'Out' " _
& "and u.user_bannerid = ?bannerID " _
& "and start.user_bannerid = ?bannerID " _
& "and ug.user_bannerid = ?bannerID " _
& "and ug.group_id = ?GroupID " _
& " and start.group_id = ?GroupID " _
& ") total "
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myCommand.Parameters.Add("?bannerID", txtBannerID.Text)
myCommand.Parameters.Add("?GroupID", cboGroups.SelectedValue)
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
dgvStatus.DataSource = myData
dgvStatus.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub