如何将下拉框中的值添加到Mysql查询中?

时间:2015-03-02 14:04:36

标签: mysql vb.net

我有一个下拉框,您可以在其中选择以下内容:

“今日”
“过去7天”

MySQL Query是:

Dim x1 As New MySqlConnection("Server=localhost;Database=test;UID=test;PWD=test;")
x1.Open()
Dim comx1 As New MySqlCommand("SELECT COUNT(*) as c FROM toutcome WHERE AffID = '" & CType(Session.Item("affID"), String) & "' AND CompletedDate= '" & DropDownList1.Text & "'", x1)
Dim myReaderx1 As MySqlDataReader = comx1.ExecuteReader(CommandBehavior.CloseConnection)
myReaderx1.Read()
Label12.Text = myReaderx1.Item(0).ToString()

Dropbox中今天的值应为“CURDATE()”

因此查询应该如下所示:

Dim x1 As New MySqlConnection("Server=localhost;Database=test;UID=test;PWD=test;")
x1.Open()
Dim comx1 As New MySqlCommand("SELECT COUNT(*) as c FROM toutcome WHERE AffID = '" & CType(Session.Item("affID"), String) & "' AND CompletedDate = CURDATE() ", x1)
Dim myReaderx1 As MySqlDataReader = comx1.ExecuteReader(CommandBehavior.CloseConnection)
myReaderx1.Read()
Label12.Text = myReaderx1.Item(0).ToString()

我该怎么做?
使用Dropbox中的值填充DropDownList1.Text

1 个答案:

答案 0 :(得分:0)

Dim commandText = "SELECT COUNT(*) as c FROM toutcome " & _
                 "WHERE AffID = '" & CType(Session.Item("affID"), String) & "' AND CompletedDate = @init "
        Using c = New MySqlConnection("Server=localhost;Database=test;UID=test;PWD=test;")
            Using com = New MySqlCommand(commandText, c)
                c.Open()
                com.Parameters.Add("@init", MySqlDbType.String).Value = Convert.ToString(DropDownList1.Text)
                Using myReader = com.ExecuteReader(CommandBehavior.CloseConnection)
                    If myReader.Read() Then
                        Label11.Text = myReader.Item(0).ToString()

                        myReader.Close()
                    End If
                End Using
            End Using
        End Using