" name"附近的语法错误

时间:2015-01-16 10:20:46

标签: vb.net sql-server-2008

conn.Open()
'MsgBox("Established!!!")

cmd.Connection = conn
cmd.CommandText = "SELECT price FROM products WHERE name '" & tb_pname.Text & "'"
dr = cmd.ExecuteReader

While dr.Read
    Me.tb_price.Text = Convert.ToString(dr("price"))
End While

conn.Close()

3 个答案:

答案 0 :(得分:1)

您错过了姓名后的=等号

WHERE name = '" & tb_pname.Text & "'"

答案 1 :(得分:0)

您的查询运行错误。你错过了=签名。

这是正确的查询

cmd.CommandText = "SELECT price FROM products WHERE name = '" + tb_pname.Text + "'"

答案 2 :(得分:0)

如果您需要查询不区分大小写,则可以使WHERE upper(name)= upper('YourString')

cmd.CommandText = "SELECT price FROM products WHERE upper(name) = upper('" & tb_pname.Text & "')"

您也可以使用“like”运算符,其中您使用“WHERE name like'PutYourTextHere'”,因此您还可以使用%之类的通配符作为任何字符串。 举个例子: 名称如'%Thomson%' 会找到包含字符串'Thomson'的任何名称。

cmd.CommandText = "SELECT price FROM products WHERE name like '" & tb_pname.Text & "'"