【问题标题】:Insert into statement error插入语句错误
【发布时间】:2016-03-12 05:28:12
【问题描述】:

试试

            Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jen\Documents\Jade\vb\database.accdb")

            txtStatus.Text = "Active"

            Dim account As String = ("Insert into Login(Username, Password, FirstName, LastName, AccountType, Status) VALUES ('" & txtUsername.Text & "' , '" & txtPass.Text & "', '" & txtFirst.Text & "', '" & txtLast.Text & "', '" & cmbType.Text & "', '" & txtStatus.Text & "')'")
            conn.Open()
            ole = New OleDbCommand(account, conn)
            ole.ExecuteNonQuery()

            MsgBox("Successfully Inserted!")

            Dim strsql2 As New OleDbCommand("select * from Login", conn)
            Dim sqlda = New OleDbDataAdapter(strsql2)
            Dim sqldataset = New DataSet
            sqlda.Fill(sqldataset)

            Me.DataGridView1.DataSource = sqldataset.Tables(0)
            conn.Close()
            DataGridView1.Refresh()

错误表明我的 insert into 语句不正确。我已经多次检查我的数据库并重新输入代码,但仍然得到同样的错误。

【问题讨论】:

    标签: vb.net insert-into


    【解决方案1】:

    我在查询中的右括号后发现了一个额外的 (') qoute。使用此查询并检查

     Dim account As String = ("Insert into Login(Username, Password, FirstName, LastName, AccountType, Status) VALUES ('" & txtUsername.Text & "' , '" & txtPass.Text & "', '" & txtFirst.Text & "', '" & txtLast.Text & "', '" & cmbType.Text & "', '" & txtStatus.Text & "')")
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助 :) 我已经找到答案 :D 单词密码应该在括号中。我正在使用访问数据库。 thnx anway ^^
    【解决方案2】:

    AccountType 和 Status 是字符串类型吗?
    如果不是,你应该这样写:

    Dim account As String = ("Insert into Login(Username, Password, FirstName, LastName, AccountType, Status) VALUES ('" & txtUsername.Text & "' , '" & txtPass.Text & "', '" & txtFirst.Text & "', '" & txtLast.Text & "', " & cmbType.Text & ", " & txtStatus.Text & ")'")
    

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      相关资源
      最近更新 更多