在sql查询中使用常量

时间:2017-02-20 10:15:15

标签: mysql sql .net vb.net visual-studio

我的代码是:

    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()

这使用另一个形式内的全局变量集的预设常量,即Input,其中input = textbox1.text。这意味着用户会在textbox1.text中输入一个值,然后将该值设置为Input。有人可以帮助我如何使用此常量来查询Where语句。

2 个答案:

答案 0 :(得分:0)

您在评论代码中几乎拥有它。修改查询以接受日期参数:

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

然后将参数添加到命令

c3.Parameters.AddWithValue("@date_param", Userinput.Text)

答案 1 :(得分: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()