INSERT 语句出错

Posted

技术标签:

【中文标题】INSERT 语句出错【英文标题】:Error on INSERT statement 【发布时间】:2013-03-12 07:58:41 【问题描述】:

我一遍又一遍地遇到错误,谁能告诉我我的 INSERT 语句有什么问题?

这是我的代码:

 Dim SQLcon As New OleDbConnection
        Dim SQLdr As OleDbDataReader
        Try
            SQLcon.ConnectionString = "Provider=Microsoft.ACE.OleDb.12.0;" & _
                                       "Data Source= C:\Users\cleanfuel\Desktop\ProgramniOrig\DBhospital.accdb"
            Dim SQLcmd As New OleDbCommand("INSERT INTO tblLogin (Username,Password,SecretQuestion,SecretAnswer)" & _
                                         "VALUES(@Username,@Password,@SecretQuestion,@SecretAnswer)", SQLcon)
            SQLcmd.Parameters.AddWithValue("@Username", txtUser.Text)
            SQLcmd.Parameters.AddWithValue("@Password", txtPass.Text)
            SQLcmd.Parameters.AddWithValue("@SecretQuestion", txtSecretQ.Text)
            SQLcmd.Parameters.AddWithValue("@SecretAnswer", txtSecretA.Text)
            SQLcon.Open()
            MsgBox("Patient Added!", MsgBoxStyle.Information)
            SQLdr = SQLcmd.ExecuteReader()
        Catch ex As Exception
            MessageBox.Show("Error Occured, Can't Register!" & ex.Message)
        Finally
            SQLcon.Close()
        End Try
        Return ""

【问题讨论】:

【参考方案1】:

尝试用[]分隔列名PASSWORD

Dim _query As String = "INSERT INTO tblLogin (Username, " & _
                       "[Password], SecretQuestion, SecretAnswer) " & _
                       "VALUES(@Username, @Password, " & _
                              "@SecretQuestion,@SecretAnswer)"
Dim SQLcmd As New OleDbCommand(_query, SQLcon)
MS ACCESS Reserved Keywords

【讨论】:

以上是关于INSERT 语句出错的主要内容,如果未能解决你的问题,请参考以下文章

数据库项目 - INSERT 语句出错

“查询输入必须至少包含一个表或查询”添加“INSERT INTO”语句时出错

校验Select和Insert的方法

使用notepad++/excle快速将cvs文件转换为insert语句技巧以及注意点

sql server 中insert into 语句,数据一律加单引号吗,还是说如是数字,可不加引号.

关于SQL语句中的insert用法