从VB.Net写入访问数据库

时间:2015-03-26 22:08:54

标签: vb.net ms-access

我正在尝试创建一个允许用户填写表单的表单,按“提交”按钮,它会将数据放入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

上面是我正在使用的代码,代码在行Command.ExecuteNonQuery()上失败,说明语法错误,但它没有给我更多信息。

更新 以下是以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 (123,TEST1000,Mythe,PPM,27/03/2015,Completed,07:48:37,07:48:43,00:00:00,00:00:06,TEST1000)"

语法错误仍然存​​在,说“07:48:37”中缺少操作数

1 个答案:

答案 0 :(得分:0)

我设法对它进行排序,感谢您的帮助

这是我使用的代码:

        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