2016-03-08 109 views
0

请帮助解决我这个问题..即时通讯非常新的这 我不能添加新的员工到表员工..每当我尝试添加它显示语法错误插入语句语法错误插入语句vb.net

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 
+0

哪条线给你错误? – Matriac

+0

[您的代码易受SQL注入影响](http://bobby-tables.com/)。不要使用字符串连接,而是使用参数化语句。 – Spidey

回答

0

替换此,

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 & "')" 

感谢 Manoj

+0

尽量不要在列名之间使用空格。而不是“员工姓名”,将“EmployeeName”写为列名 – Manoj