2012-10-08 132 views
0

我無法從DataGridView向Access數據庫插入數據。它顯示INSERT INTO語句中的語法錯誤。請幫幫我。這是我的代碼:將數據從DatagridView插入Access數據庫VB.NET

Try 
    com = New OleDb.OleDbCommand 
    com.Connection = con 
    Dim atdate As Date 
    Dim id As String 
    Dim name As String 
    Dim time As String 
    Dim status As String 
    For x As Integer = 0 To ATCGRID.Rows.Count - 1 
     atdate = ATCGRID.Rows(x).Cells(2).Value 
     id = ATCGRID.Rows(x).Cells(0).Value 
     Name = ATCGRID.Rows(x).Cells(1).Value 
     time = ATCGRID.Rows(x).Cells(3).Value 
     status = ATCGRID.Rows(x).Cells(4).Value 
     con.Open() 
     str1 = "INSERT INTO EMP_ATTENDANCE(DATE,EMP_ID,EMP_NAME,EMP_TIME,EMP_STATUS)values(@DATE,@EMP_ID,@EMP_NAME,@EMP_TIME,@EMP_STATUS)" 
     Dim com As New OleDb.OleDbCommand(str1, con) 
     com.Parameters.AddWithValue("@DATE", atdate) 
     com.Parameters.AddWithValue("@EMP_ID", id) 
     com.Parameters.AddWithValue("@EMP_NAME", Name) 
     com.Parameters.AddWithValue("@EMP_TIME", time) 
     com.Parameters.AddWithValue("@EMP_STATUS", status) 
     com.ExecuteNonQuery() 
     com.Dispose() 
    Next 
    con.Close() 
    MessageBox.Show("Registered Successfully!", "Register", MessageBoxButtons.OK, MessageBoxIcon.Information) 
Catch ex As OleDb.OleDbException 
    MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error") 
Catch ex As Exception 
    MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error") 
End Try 

回答

0

「Date」很可能是關鍵字。嘗試將它放在括號中:在您的SQL文本中。

+0

THNX LarsTech .... THNK U SOUX ... ITZ WORKING ... – Thanzeem

+0

我強制取出con.Open()從循環... nw itz工作.... – Thanzeem

+1

@Thanzeem很高興它的工作。你可能應該用'Using'語句封裝所有的數據對象,這會爲你處理對象。請參閱[本示例](http://stackoverflow.com/a/6348172/719186) – LarsTech