【问题标题】:check the manual that corresponds to your mariadb server version for the right syntax to use near "user" at line 1检查与您的 mariadb 服务器版本相对应的手册,以在第 1 行的“用户”附近使用正确的语法
【发布时间】:2016-12-06 16:50:00
【问题描述】:

我的表单中有一个文本框、按钮和 datagridview。当我单击按钮时,系统将根据我的文本框从数据库中抓取一个表格并显示在 datagridview 上。

单击按钮时出现此错误。我哪里错了?

这是我的 dbconn

Module mod_dbconn
Public conn As MySqlConnection
Public Sub openDB()
    Dim dbname As String = scr_sales.btn_dbswitch.Text
    Dim server As String = "localhost"
    Dim user As String = "root"
    Dim password As String = ""

    Try
        conn = New MySqlConnection
        conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, user, password, dbname)

        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

这是我的表格

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim query As String = "SELECT * FROM '" + TextBox1.Text + "'"
    Dim cmd As New MySqlCommand(query, conn)
    Dim da As New MySqlDataAdapter(cmd)
    Dim dt = New DataTable
    Dim cb As MySqlCommandBuilder



    cb = New MySqlCommandBuilder(da)
    DataGridView1.Refresh()

    Try
        conn.Open()
        da.Fill(dt)

        Dim bsource As New BindingSource
        bsource.DataSource = dt

        Me.DataGridView1.DataSource = bsource
        da.Update(dt)

        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()
    End Try
End Sub

【问题讨论】:

    标签: mysql vb.net syntax-error


    【解决方案1】:

    您正在尝试构建一个动态表格选择,因此对于表格名称,您不需要在表格名称周围加上引号

     "SELECT * FROM " + TextBox1.Text + " ;" 
    

    【讨论】:

    • 天哪,我在这个问题上花了半天时间......现在你告诉我我需要做的就是删除引号......终于解决了这个问题,非常感谢!!!
    • @vbnewbie 。有时树叶会让你看不到森林
    猜你喜欢
    • 2021-09-27
    • 2019-05-31
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    相关资源
    最近更新 更多