2017-02-20 69 views
0

我的代碼是恆定內的:使用SQL查詢

Dim c3 As MySqlCommand 
    Dim q3 As String = "SELECT  date 
     FROM   `river-derwent-keswick-portinscale` 
    WHERE(`date` = Input)" 
    c3 = New MySqlCommand(q3, conn) 
    'c3.Parameters.AddWithValue("@Date", Userinput.Text) 
    'Userinput.Text Is a textbox 
    ' If a field if found where the date matches the userinput 
    ' Output value to textbox 
    Dim DR3 As MySqlDataReader = c3.ExecuteReader() 
    If DR3.Read Then 
     Datetxt.Text = DR3.GetValue(0) 
    End If 
    DR3.Close() 

這使用另一種形式的內部設置一個全局變量的預先設定的常數,其是輸入,其中輸入= textbox1.text。這意味着用戶將輸入一個值到textbox1.text中,然後將該值設置爲Input。有人可以幫助我如何使用這個常量來查詢Where語句。

回答

0

你差不多在評論代碼中。修改查詢,以接受日期的說法:

SELECT date 
FROM `river-derwent-keswick-portinscale` 
WHERE(`date` = @date_param) 

然後參數添加到命令

c3.Parameters.AddWithValue("@date_param", Userinput.Text) 
0
Dim c3 As MySqlCommand 
Dim q3 As String = "SELECT date FROM `river-derwent-keswick-portinscale` WHERE(`date` = @Date)" 
c3 = New MySqlCommand(q3, conn) 
c3.Parameters.AddWithValue("@Date", Userinput.Text) 
'Userinput.Text Is a textbox 
' If a field if found where the date matches the userinput 
' Output value to textbox 
Dim DR3 As MySqlDataReader = c3.ExecuteReader() 
If DR3.Read Then 
    Datetxt.Text = DR3.GetValue(0) 
End If 
DR3.Close()