将数据从 DatagridView 插入到 Access 数据库 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12782988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:20:36  来源:igfitidea点击:

Insert data From DatagridView to Access Database VB.NET

vb.net

提问by Thanzeem

I cannot insert data from DataGridView to Access database. It shows Syntax Error in INSERT INTO Statement. Please help me. This is my code:

我无法将数据从 DataGridView 插入到 Access 数据库中。它显示 INSERT INTO 语句中的语法错误。请帮我。这是我的代码:

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

采纳答案by LarsTech

"Date" is most likely a keyword. Try placing it in brackets: [DATE]in your SQL text.

“日期”很可能是一个关键字。尝试将它放在括号中:[DATE]在您的 SQL 文本中。