如何使用 VB 和 SQL 将记录添加到 Access 中的关系数据库

How to use VB and SQL to add Records to a Relational Database in Access

我正在使用 Visual Basic 创建一个表单应用程序作为大学项目的一部分。我已经使用 SQL 语句从访问数据库中读取,但在写入时遇到了一些问题。我想冒险猜测这是由于数据库在表之间存在关系。

这是我第一次尝试做一些实质性的事情,vb 不是我选择的语言。期望代码充其量是糟糕的。如果有人有指向我可以用来改进的资源的链接,我将不胜感激。

异常:

Exception thrown: 'System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.'

Exception location: 'commAddToStaff.ExecuteNonQuery()'

当 运行 时,两个 try 语句都在捕获异常。我尝试在参数中提供数据而不是使用文本框中的数据,但这并没有解决问题。

代码:

Private Sub btnAddStaffMember_Click(sender As Object, e As EventArgs) Handles btnAddStaffMember.Click

    'Dimension tblStaff Parameters 
    Dim AddEmployeeIDParam As New OleDb.OleDbParameter("@AddEmployeeID", txtAddEmployeeID.Text)
    Dim AddForenameParam As New OleDb.OleDbParameter("@AddForename", txtAddForename.Text)
    Dim AddSurnameParam As New OleDb.OleDbParameter("@AddSurname", txtAddSurname.Text)
    Dim AddDOBParam As New OleDb.OleDbParameter("@AddDOB", txtAddDOB.Text)
    Dim AddUserTierParam As New OleDb.OleDbParameter("@AddUserTier", txtAddUserTier.Text)

    'Dimension tblContacts Parameters
    Dim conContact As New OleDb.OleDbConnection("Provider=Microsoft.......")
    Dim commContactCount As New OleDb.OleDbCommand("Select Count(*) FROM tblContacts", conContact)
    commContactCount.Connection.Open()                 
    Dim ContactID = commContactCount.ExecuteScalar + 1     'Calculate the contactID of the new record
    commContactCount.Connection.Close()                  'Close the connection
    Dim AddContactIDParam As New OleDb.OleDbParameter("@AddContactID", ContactID)
    Dim AddAddressParam As New OleDb.OleDbParameter("@AddAddress", txtAddAddress.Text)
    Dim AddPostcodeParam As New OleDb.OleDbParameter("@AddPostcode", txtAddPostcode.Text)
    Dim AddEmailParam As New OleDb.OleDbParameter("@AddEmail", txtAddEmail.Text)
    Dim AddMobileNoParam As New OleDb.OleDbParameter("@AddMobileNo", txtAddMobileNumber.Text)

    Dim conAddToStaff As New OleDb.OleDbConnection("Provider=Microsoft....")
    Dim commAddToStaff As New OleDb.OleDbCommand("Insert Into tblStaff (EmployeeID, Forename, Surname, DOB, User_Tier, ContactID) Values (@AddEmployeeID, @AddForename, @AddSurname, @AddDOB, @AddUserTier, @AddContactID)", conAddToStaff)
    commAddToStaff.Parameters.Add(AddEmployeeIDParam)
    commAddToStaff.Parameters.Add(AddForenameParam)
    commAddToStaff.Parameters.Add(AddSurnameParam)
    commAddToStaff.Parameters.Add(AddDOBParam)
    commAddToStaff.Parameters.Add(AddUserTierParam)

    Dim commAddToContact As New OleDb.OleDbCommand("Insert Into tblContacts (ContactID, Address, Postcode, Email, Mobile_Number) Values (@AddContactID, @AddAddress, @AddPostcode, @AddEmail, @AddMobileNo)", conContact)
    commAddToContact.Parameters.Add(AddContactIDParam)
    commAddToContact.Parameters.Add(AddAddressParam)
    commAddToContact.Parameters.Add(AddPostcodeParam)
    commAddToContact.Parameters.Add(AddEmailParam)
    commAddToContact.Parameters.Add(AddMobileNoParam)

    Try
        commAddToStaff.Connection.Open()                 'Open a connection to the database
        commAddToStaff.ExecuteNonQuery()                 'Execute the command
        commAddToStaff.Connection.Dispose()              'Remove unmanaged resources
        commAddToStaff.Connection.Close()                'Close the connection
    Catch ex As Exception
        MessageBox.Show("Error with staff")
    End Try

    Try
        commAddToContact.Connection.Open()                 'Open a connection to the database
        commAddToContact.ExecuteNonQuery()                 'Execute the command
        commAddToContact.Connection.Dispose()              'Remove unmanaged resources
        commAddToContact.Connection.Close()                'Close the connection
    Catch ex As Exception
        MessageBox.Show("Error with contacts")
    End Try

    MessageBox.Show("Reached")

    Me.Hide()       'Close the Current screen
    StaffDB_Add_Staff_Security_Question.Show()  'Open the Add Security Question Screen
End Sub

您要在此处的六列中插入六个值:

Dim commAddToStaff As New OleDb.OleDbCommand("Insert Into tblStaff (EmployeeID, Forename, Surname, DOB, User_Tier, ContactID) Values (@AddEmployeeID, @AddForename, @AddSurname, @AddDOB, @AddUserTier, @AddContactID)", conAddToStaff)

但是你在这里只给命令添加了五个参数:

commAddToStaff.Parameters.Add(AddEmployeeIDParam)
commAddToStaff.Parameters.Add(AddForenameParam)
commAddToStaff.Parameters.Add(AddSurnameParam)
commAddToStaff.Parameters.Add(AddDOBParam)
commAddToStaff.Parameters.Add(AddUserTierParam)

SQL 代码中 @AddContactID 占位符的参数在哪里?

编辑:

郑重声明,以下是我倾向于为此类任务编写代码的方式,忽略了生成 ContactID 值的可怕方式:

Using connection As New OleDbConnection("connection string here")
    connection.Open()

    Dim contactCount As Integer

    Using contactCountCommand As New OleDbCommand("SELECT COUNT(*) FROM tblContacts", connection)
        contactCount = CInt(contactCountCommand.ExecuteScalar())
    End Using

    Dim contactId = contactCount + 1

    Using staffCommand As New OleDbCommand("INSERT INTO tblStaff (EmployeeID, Forename, Surname, DOB, User_Tier, ContactID) Values (@EmployeeID, @Forename, @Surname, @DOB, @User_Tier, @ContactID)", connection)
        With staffCommand.Parameters
            .Add("@EmployeeID", OleDbType.VarChar, 50).Value = txtAddEmployeeID.Text
            .Add("@Forename", OleDbType.VarChar, 50).Value = txtAddForename.Text
            .Add("@Surname", OleDbType.VarChar, 50).Value = txtAddSurname.Text
            .Add("@DOB", OleDbType.Date).Value = CDate(txtAddDOB.Text) 'Why isn't this coming from a DateTimePicker?
            .Add("@User_Tier", OleDbType.VarChar, 50).Value = txtAddUserTier.Text
            .Add("@ContactID", OleDbType.Integer).Value = contactId
        End With

        staffCommand.ExecuteNonQuery()
    End Using

    Using contactCommand As New OleDbCommand("INSERT INTO tblContacts (ContactID, Address, Postcode, Email, Mobile_Number) Values (@ContactID, @Address, @Postcode, @Email, @Mobile_Number)", connection)
        With contactCommand.Parameters
            .Add("@ContactID", OleDbType.Integer).Value = contactId
            .Add("@Address", OleDbType.VarChar, 50).Value = txtAddAddress.Text
            .Add("@Postcode", OleDbType.VarChar, 50).Value = txtAddPostcode.Text
            .Add("@Email", OleDbType.VarChar, 50).Value = txtAddEmail.Text
            .Add("@Mobile_Number", OleDbType.VarChar, 50).Value = txtAddMobileNumber.Text
        End With

        contactCommand.ExecuteNonQuery()
    End Using
End Using

通过将代码重新排列成更小的部分(相关项彼此靠近),可以更容易地看出哪里添加的参数不足。类似于:

Imports System.Data.OleDb

Public Class Form1

    Dim connStr As String = "Provider=Microsoft......."

    Sub AddStaffMemberToDatabase(contactId As Integer)
        Dim sql = "INSERT INTO tblStaff (EmployeeID, Forename, Surname, DOB, UserTier, ContactID) VALUES (@AddEmployeeID, @AddForename, @AddSurname, @AddDOB, @AddUserTier, @AddContactID)"

        Using conn As New OleDbConnection(connStr),
                cmd As New OleDbCommand(sql, conn)

            Dim dob = DateTime.Parse(txtAddDOB.Text)

            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddEmployeeID", .OleDbType = OleDbType.VarChar, .Size = 20, .Value = txtAddEmployeeID.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddForename", .OleDbType = OleDbType.VarWChar, .Size = 255, .Value = txtAddForename.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddSurname", .OleDbType = OleDbType.VarWChar, .Size = 255, .Value = txtAddSurname.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddDOB", .OleDbType = OleDbType.Date, .Value = dob})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddUserTier", .OleDbType = OleDbType.VarChar, .Size = 20, .Value = txtAddUserTier.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddContactID", .OleDbType = OleDbType.Integer, .Value = contactId})

            conn.Open()
            cmd.ExecuteNonQuery()

        End Using

    End Sub

    Sub AddContactToDatabase(contactId As Integer)
        Dim sql = "INSERT INTO tblContacts (ContactID, Address, Postcode, Email, Mobile_Number) VALUES (@AddContactID, @AddAddress, @AddPostcode, @AddEmail, @AddMobileNo)"

        Using conn As New OleDbConnection(connStr),
                cmd As New OleDbCommand(sql, conn)

            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddContactID", .OleDbType = OleDbType.Integer, .Value = contactId})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddAddress", .OleDbType = OleDbType.VarWChar, .Size = 255, .Value = txtAddAddress.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@txtAddPostcode", .OleDbType = OleDbType.VarChar, .Size = 20, .Value = txtAddPostcode.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddEmail", .OleDbType = OleDbType.VarWChar, .Size = 255, .Value = txtAddEmail.Text})
            cmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@AddMobileNo", .OleDbType = OleDbType.VarChar, .Size = 20, .Value = txtAddMobileNumber.Text})

            conn.Open()
            cmd.ExecuteNonQuery()

        End Using

    End Sub

    Sub AddStaffMember()
        Dim sql = "SELECT COUNT(*) FROM tblContacts"
        Dim contactID As Integer

        Using conn As New OleDbConnection(connStr),
                cmd As New OleDbCommand(sql, conn)
            conn.Open()
            contactID = Convert.ToInt32(cmd.ExecuteScalar()) + 1
        End Using

        AddStaffMemberToDatabase(contactID)
        AddContactToDatabase(contactID)

    End Sub

    Private Sub btnAddStaffMember_Click(sender As Object, e As EventArgs) Handles btnAddStaffMember.Click
        AddStaffMember()
        Me.Hide()
        StaffDB_Add_Staff_Security_Question.Show()  'Open the Add Security Question Screen

    End Sub

End Class

Using 语句确保 "unamanaged resources" 在代码完成后被释放。

请注意,您需要提供更具体、更可靠的 DOB 文本解析方式(例如 DateTime.TryParseExact)。此外,需要编辑数据库类型和大小以匹配数据库中的声明。