无法将数据保存到Access数据库

时间:2014-04-07 12:10:51

标签: vb.net ms-access

我从其他来源学习了这个编程,我也下载了他的文件进行测试是否有效。我和他做了同样的事情,但是我的工作并没有出现任何错误。 任何资深人士都可以帮我做一些纠正或调试。

这是我的代码:

Dim Command As New OleDb.OleDbCommand
Dim Adapter As New OleDb.OleDbDataAdapter
Dim Connection As OleDb.OleDbConnection = AConnection()

Public Function AConnection() As OleDb.OleDbConnection
    Return New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Music_Sales_Database.mdb")
End Function

Private Sub SendCustomerInformationToDatabase()
    Connection.Open()
    With Command
        .Connection = Connection
        .CommandText = "INSERT into Customer(Customer_ID,First_Name,Last_Name,Contact_Number,Email_Address) " & _
            "Values('" & txtCustomerID.Text & "','" & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtContactNumber.Text & "','" & _
        "','" & txtEMail.Text & "')"

    End With
    Connection.Close()
End Sub

Private Sub CheckOut_Reservation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    txtDateOfReservation.Text = Date.Today
    txtCustomerID.Focus()
    InputText = txtContactNumber.Text
    NumericCheck = IsNumeric(InputText)

    Connection.Open()
    With Command
        .Connection = Connection
        .CommandText = "SELECT * from Customer"
    End With
    Dim Table As New DataTable

    Adapter.SelectCommand = Command
    Adapter.Fill(Table)

    Connection.Close()
    Adapter.Dispose()

End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

    SendCustomerInformationToDatabase()

    Dim CheckOutSound As String
    Dim path = System.Windows.Forms.Application.StartupPath
    Dim MyPlayer As New SoundPlayer()

    CheckOutSound = "\Success.wav"
    MyPlayer.SoundLocation = path & CheckOutSound
    MyPlayer.Play()

    If AInputCorrect = True And BInputCorrect = True And CInputCorrect = True And DInputCorrect = True And EInputCorrect = True Then
        FInputCorrect = True
    Else
        FInputCorrect = False
    End If

    If FInputCorrect = True Then
        MessageBox.Show("Reservation Submitted.", "Reservation Successful", MessageBoxButtons.OK, MessageBoxIcon.None)
    End If

    If FInputCorrect = False Then
        MessageBox.Show("Please check your Customer Information and try again.", "Reservation Failed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End If

    'Me.Close()
End Sub

谢谢

1 个答案:

答案 0 :(得分:0)

据我所见,你没有执行你的命令:

Private Sub SendCustomerInformationToDatabase()
    Connection.Open()
    With Command
        .Connection = Connection
        .CommandText = "INSERT into Customer(Customer_ID,First_Name,Last_Name,Contact_Number,Email_Address) " & _
         "Values('" & txtCustomerID.Text & "','" & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtContactNumber.Text & "','" & _
         "','" & txtEMail.Text & "')"

    End With

'Execute my newly created command
    Command.ExecuteNonQuery
    Connection.Close()
End Sub

MSDN - ExecuteNonQuery

请注意,如果您的查询包含语法错误,则可能会遇到更多异常。

请记住,像这样构建SQL-Query实际上是 BAD 练习。我建议你看一下以下的链接

Stack Overflow: Preventing SQL Injection

Code Project: Sql Injection Attacks

Using Parameterized queries