MySQL 服务器版本,用于在 'where no =' 计数附近使用正确的语法

标签 mysql vb.net vb.net-2010

我有这个问题:

MySQL server version for the right syntax to use near 'where no ='count'' at line 1

这是我的代码:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
    Dim Reader As MySqlDataReader

    MysqlConn.Open()

    Dim query As String
    Dim spath As String
    Dim count As Integer = 0
    Dim mysound As Media.SoundPlayer
    Dim cmd As MySqlCommand
    Dim rdr As MySqlDataReader


    query = "Select * from penjara.info"
    Command = New MySqlCommand(query, MysqlConn)
    Reader = Command.ExecuteReader



    While Reader.Read
        Reader.Close()
        count = count + 1
        query = "Select penjara.info where  no ='count'"
        cmd = New MySqlCommand(query, MysqlConn)
        rdr = cmd.ExecuteReader
    End While
End Sub

如何解决这个错误?

最佳答案

试试这个:

query = "Select * from penjara.info where  no = '" & count & "'"

这将传入变量count的值。但是,请注意,这是一种不安全的方法,因为连接形成查询会使您的应用程序容易受到 SQL 注入(inject)的攻击。相反,您应该使用参数化查询。

Dim connStr As String = "server=localhost;userid=root;password=;database=penjara"
Using MySqlConn As New MySqlConnection(connStr)
Using cmd As New MySqlCommand()
    With cmd
        .Connection = MySqlConn
        .CommandText = "Select * from penjara.info where  no = @count"
        .CommandType = CommandType.Text
        .Parameters.AddWithValue("@count", count)
    End With
    Try
        MySql.Open()
        rdr = cmd.ExecuteReader
    Catch ex As Exception
        <handle exception>
    End Try
End Using
End Using

关于MySQL 服务器版本,用于在 'where no =' 计数附近使用正确的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24904541/

相关文章:

vb.net - 计算两个日期之间的时间?

mysql - 获取类型为 "text"的模型属性以在 mysql 中使用 MEDIUMTEXT 列 dataType

jquery - 不正确的 Sequelize 查询

返回多行的Mysql子查询

c# - 设置字符串长度限制然后填充子字符串或空格

VB.NET,在Windows资源管理器中打开特定文件夹?

mysql - 仅在 vb.net 中查询同一按钮

mysql - 如何启动mysql2服务器?

MySQL 数据读取器未进入循环

vb.net - 在VB中查找函数中的错误