我正在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
答案 0 :(得分:0)
您可以通过单击按钮时检查文本框来执行此操作。复杂的查询可能很难直接使用,但它归结为构建您想要的命令字符串:(简化方式)
查询字符串构建:
Dim SQL As String = "SELECT SomeFields FROM TableWhatever WHERE Date=?Date"
If txtName.Text.Trim <> "" Then
SQL &= " Name=?Name"
End If
如果在where子句
中添加参数 ... Connection stuff here
myCommand.CommandText = SQL
If txtname.text.Trim <> "" Then
myCommand.Parameters.Add("?Name", txtName.Text.Trim)
End If