【问题标题】:syntax error insert into statement vb.net语法错误插入语句 vb.net
【发布时间】:2016-03-08 06:06:32
【问题描述】:

请帮我解决这个问题..我对此很陌生 我无法将新员工添加到表employee ..每当我尝试添加它时都会显示语法错误插入语句

Public Class AddNewEmployee

    Dim dr As OleDbDataReader
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim conn As New OleDbConnection(My.Settings.rayshadatabaseConnectionString)
    Dim cmd As OleDbCommand

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        conn.Open()
        Try
            Dim str As String = "INSERT INTO employee" _
            & "(Employee Name, IC Number, HP Number, Address)" _
            & " Values (" _
            & "'" & txtEmployeeName.Text & "', " _
            & "'" & txtIC_Number.Text & "'," _
            & "'" & txtHP_Number.Text & "'," _
            & "'" & txtAddress.Text & "')"

            cmd = New OleDbCommand(str, conn)
            Dim i As Integer = cmd.ExecuteNonQuery()
            If i > 0 Then
                MessageBox.Show("Record Succesfully added.", "Process Completed", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Adding failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
            cmd.Dispose()
        End Try
        frmEmployee.loadR()
        Me.Close()
    End Sub
End Class

【问题讨论】:

标签: vb.net


【解决方案1】:

替换这个,

    Dim str As String = "INSERT INTO employee" _
    & "(Employee Name, IC Number, HP Number, Address)" _
    & " Values (" _
    & "'" & txtEmployeeName.Text & "', " _
    & "'" & txtIC_Number.Text & "'," _
    & "'" & txtHP_Number.Text & "'," _
    & "'" & txtAddress.Text & "')"

有了这个,

Dim str As String = "INSERT INTO employee" _
    & "([Employee Name], [IC Number], [HP Number], [Address])" _
    & " Values (" _
    & "'" & txtEmployeeName.Text & "', " _
    & "'" & txtIC_Number.Text & "'," _
    & "'" & txtHP_Number.Text & "'," _
    & "'" & txtAddress.Text & "')"

谢谢 马诺杰

【讨论】:

  • 尽量不要在列名之间使用空格。代替“员工姓名”,将“员工姓名”写为列名
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 2015-11-16
  • 1970-01-01
  • 2016-06-10
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多