2015-03-02 38 views
0

我有一个下拉框,您可以选择以下内容:如何将下拉框中的值添加到Mysql查询中?

“今天”
“最近7天”

MySQL查询是:

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

+0

到目前为止,2人已经编辑了这个问题,没有人试图回答或协助。如果你不能协助,请不要束缚我的时间。 Stackoverflow也适用于像我这样需要帮助和教育的人。 – MatHatrik 2015-03-04 15:33:06

回答

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 
相关问题