从VB.Net写入Access数据库

从VB.Net写入Access数据库,vb.net,ms-access,Vb.net,Ms Access,我正在尝试创建一个表单,允许用户填写表单,按下Submit按钮,然后将数据放在Access数据库的新行中 Public Sub Inject_To_Database() Dim sqlConnection1 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Z.USB File Storage\Visual Studio Projects\Job Tracker 2\Job Trac

我正在尝试创建一个表单,允许用户填写表单,按下Submit按钮,然后将数据放在Access数据库的新行中

  Public Sub Inject_To_Database()
        Dim sqlConnection1 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Z.USB File Storage\Visual Studio Projects\Job Tracker 2\Job Tracker 2\Database Job Records.accdb")


        sqlConnection1.Open()

        Dim Sqlstr = "INSERT INTO `Job Records` (`Job Number`, `Job Description`, `Site`, `Type Of Work" & _
                "`, `Job Date`, `Outcome Of Job`, `Start Time`, `End Time`, `Park" & _
                "ed Time`, `Completion Time`, `Allocated Time`, `Comment Sent & _
                "`)" & _
       "VALUES (" & Form1.SQL_JobNumber.Text & "," & _
       Form1.SQL_JobDescription.Text & "," & _
       Form1.SQL_Site.Text & "," & _
       Form1.SQL_TypeOfWork.Text & "," & _
       Form1.SQL_StartDate.Text & "," & _
       Form1.SQL_JobOutcome.Text & "," & _
           Form1.SQL_StartTime.Text & "," & _
           Form1.SQL_EndDate.Text & "," & _
           Form1.SQL_ParkedTime.Text & "," & _
           Form1.SQL_CompletedTime.Text & "," & _
           Form1.SQL_CommentSent.Text & ")"

        Dim Command = New OleDbCommand(Sqlstr, sqlConnection1)
        Command.ExecuteNonQuery()
        sqlConnection1.Close()
    End Sub
上面是我正在使用的代码,代码在命令行中失败。ExecuteOnQuery声明语法错误,但它没有提供更多信息

更新: 下面是以Sqlstr结尾的字符串


语法错误仍然存在,表示07:48:37中缺少一个操作数。谢谢您的帮助,我成功地对它进行了排序

以下是我使用的代码:

        If Form1.SQL_DocumentSupport.Text = Nothing Then Form1.SQL_DocumentSupport.Text = "None"

        Dim sqlConnection1 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Z.USB File Storage\Visual Studio Projects\Job Tracker 2\Job Tracker 2\Database Job Records.accdb")



        sqlConnection1.Open()

        Dim Sqlstr = "INSERT INTO [Job Records] ([Job Number], [Job Description], [Site], [Type Of Work]" & _
                ", [Job Date], [Outcome Of Job], [Start Time], [End Time], [Parked Time], " & _
                "[Completion Time], [Allocated Time], [Comment Sent], [Document Support]" & _
                ")" & _
       "VALUES (" & CInt(Form1.SQL_JobNumber.Text) & "," & _
"""" & Form1.SQL_JobDescription.Text & """," & _
"""" & Form1.SQL_Site.Text & """," & _
"""" & Form1.SQL_TypeOfWork.Text & """," & _
"""" & Form1.SQL_StartDate.Text & """," & _
"""" & Form1.SQL_JobOutcome.Text & """," & _
"""" & Form1.SQL_StartTime.Text & """," & _
"""" & Form1.SQL_EndTime.Text & """," & _
"""" & Form1.SQL_ParkedTime.Text & """," & _
"""" & Form1.SQL_CompletedTime.Text & """," & _
"""" & Form1.SQL_AllocatedTime.Text & """," & _
"""" & Form1.SQL_CommentSent.Text & """," & _
"""" & Form1.SQL_DocumentSupport.Text & """)"

        'Form1.SQL_DocumentSupport.Text & _
        'VALUES (@JobNumber, @JobDescription, @Site, @TypeOfWork, @JobDate, @Outcome, @JobAttempts, @StartTime," & _
        '"@EndTime, @ParkedTime, @CompletionTime, @AllocatedTime, @CommentSent, @DocumentSupport)"

        Dim Command = New OleDbCommand(Sqlstr, sqlConnection1)
        Command.ExecuteNonQuery()
        sqlConnection1.Close()
    End Sub

在此处搜索[.net]参数化查询并学习如何使用它们,而不是连接SQL。它将避免这些难以追踪的错误,更重要的是防止SQL注入。当您的SQL中出现语法错误时,您需要在生成Sqlstr之后,但在调用ExecuteOnQuery之前检查它,这样您就可以看到构建它的所有噪音的最终结果,这样您就可以找出问题所在。我们不能这样做,因为我们没有您的数据或变量的内容。Sqlstr需要以as string结束。您必须关闭Form1.SQL\u DocumentSupport.Text之后的括号&此外,根据Jet/ACE SQL方言,值字符串需要用引号和带有数字符号的日期时间字段括起来。
        If Form1.SQL_DocumentSupport.Text = Nothing Then Form1.SQL_DocumentSupport.Text = "None"

        Dim sqlConnection1 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Z.USB File Storage\Visual Studio Projects\Job Tracker 2\Job Tracker 2\Database Job Records.accdb")



        sqlConnection1.Open()

        Dim Sqlstr = "INSERT INTO [Job Records] ([Job Number], [Job Description], [Site], [Type Of Work]" & _
                ", [Job Date], [Outcome Of Job], [Start Time], [End Time], [Parked Time], " & _
                "[Completion Time], [Allocated Time], [Comment Sent], [Document Support]" & _
                ")" & _
       "VALUES (" & CInt(Form1.SQL_JobNumber.Text) & "," & _
"""" & Form1.SQL_JobDescription.Text & """," & _
"""" & Form1.SQL_Site.Text & """," & _
"""" & Form1.SQL_TypeOfWork.Text & """," & _
"""" & Form1.SQL_StartDate.Text & """," & _
"""" & Form1.SQL_JobOutcome.Text & """," & _
"""" & Form1.SQL_StartTime.Text & """," & _
"""" & Form1.SQL_EndTime.Text & """," & _
"""" & Form1.SQL_ParkedTime.Text & """," & _
"""" & Form1.SQL_CompletedTime.Text & """," & _
"""" & Form1.SQL_AllocatedTime.Text & """," & _
"""" & Form1.SQL_CommentSent.Text & """," & _
"""" & Form1.SQL_DocumentSupport.Text & """)"

        'Form1.SQL_DocumentSupport.Text & _
        'VALUES (@JobNumber, @JobDescription, @Site, @TypeOfWork, @JobDate, @Outcome, @JobAttempts, @StartTime," & _
        '"@EndTime, @ParkedTime, @CompletionTime, @AllocatedTime, @CommentSent, @DocumentSupport)"

        Dim Command = New OleDbCommand(Sqlstr, sqlConnection1)
        Command.ExecuteNonQuery()
        sqlConnection1.Close()
    End Sub