需要帮助sql语法vb.net [英] Need help with sql syntax vb.net

查看:56
本文介绍了需要帮助sql语法vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试通过我的vb.net应用程序运行SQL查询但是我的查询中存在语法错误让我感到难过......这是代码:

Hi all, I'm trying to run an SQL query through my vb.net application but there is a syntax error in my query that has got me stumped... here's the code:

        Dim answer As String = comboBoxSpecies.Text

        Dim query As String =" SELECT SUM(stayCost)FROM stay WHERE petID in(SELECT petID from pet where species =)" &安培;回答

        Dim totalCost As Double = da.Getstay(查询)

        lblTotalAmountPaidOutput.Text = totalCost

正如你所看到我试图将一个字符串变量附加到我的sql查询中,该变量是从一个组合框选择中定义的,但是当我运行时我收到以下错误:

As you can see im attempting to attach a string variable to my sql query, the variable is define from a combo box selection, but when i run it i get the following error:

'你的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以便在第1行'''狗''附近使用正确的语法

(变量"answer")包含字符串'dog')

我试过在没有运气的地方移动逗号和括号

Ive tried moving the commas and parenthesis all over the place with no luck

如果有人能在我的语法中看到错误并让我知道会非常感谢,谢谢。

If anyone can see the error in my syntax and let me know it would be much appreciated, thanks.

推荐答案

¥b $ b最好使用参数,如下所示:

Hi,
is better to use parameters, like this:

        Dim answer As String = comboBoxSpecies.Text
        Dim query As String = "SELECT SUM(stayCost) FROM stay WHERE petID in (SELECT petID from pet where species=@answer)"

Dim totalCost As Double
Using cmd As New MySqlCommand(query, connection)
  cmd.Parameters.AddWithValue("@answer", answer)
  totalCost = CType(cmd.ExecuteScaler, Double)
End Using

        lblTotalAmountPaidOutput.Text = totalCost


这篇关于需要帮助sql语法vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆