谁能帮助我通过Visual Basic更新Microsoft Access中的记录?

时间:2019-02-05 13:44:50

标签: vb.net ms-access

当前,我有一段代码可以让我搜索信息并将信息添加到Microsoft Access文档中。当我尝试更新信息时会发生问题,它会通过else语句,但对Access文档没有任何作用。我的SQL出问题了吗?还是有其他可能的因素影响代码?

Private Sub BtnSaveStudent_Click(sender As Object, e As EventArgs) Handles BtnSaveStudent.Click

    If DbConnect() Then
        Dim SQLCmd As New OleDbCommand

        If CurrentStudentID = -1 Then
            'Add student
            SQLCmd.Connection = cn
            SQLCmd.CommandText = "Insert into Student (Surname, FirstNames, DateOfBirth, PostCode) " &
                                    "Values (@Surname, @FirstNames, @DateOfBirth, @PostCode)"
            SQLCmd.Parameters.AddWithValue("@Surname", TxtSurname.Text)
            SQLCmd.Parameters.AddWithValue("@FirstNames", TxtFirstName.Text)
            SQLCmd.Parameters.AddWithValue("@DateOfBirth", DatDOB.Value.Date)
            SQLCmd.Parameters.AddWithValue("@PostCode", TxtPostCode.Text)
            SQLCmd.ExecuteNonQueryAsync()


            SQLCmd.CommandText = "Select @@Identity"
            CurrentStudentID = SQLCmd.ExecuteScalar
            LblStudentID.Text = CurrentStudentID
        Else
            'Edit Student
            SQLCmd.Connection = cn
            SQLCmd.CommandText = "Update Student " &
                                "Set Surname = @Surname, " &
                                "FirstNames = @FirstNames, " &
                                "DateOfBirth = @DateOfBirth, " &
                                "PostCode = @PostCode, " &
                                "where StudentID = @CurrentStudentID"
            SQLCmd.Parameters.AddWithValue("@Surname", TxtSurname.Text)
            SQLCmd.Parameters.AddWithValue("@FirstNames", TxtFirstName.Text)
            SQLCmd.Parameters.AddWithValue("@PostCode", TxtPostCode.Text)
            SQLCmd.Parameters.AddWithValue("@DateOfBirth", DatDOB.Value.Date)
            SQLCmd.Parameters.AddWithValue("@CurrentStudentID", CurrentStudentID)
            SQLCmd.ExecuteNonQueryAsync()

        End If
        cn.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

请启用“严格选项”。 您可以在以下位置找到此设置 工具菜单->选项->项目和解决方案-> VB默认值。 这样可以避免在运行时出现错误。

将参数与Access结合使用的重要之处在于,将参数按照在SQL语句中出现的顺序输入到参数集合中。

Private Sub OPCode()
    Dim CurrentStudentID As Integer
    'A Using block ensures that your database objects closed and disposed
    'even if there is an error.
    Using cn As New OleDbConnection("Your connection string")
        Using SQLCmd As New OleDbCommand
            SQLCmd.Connection = cn
            SQLCmd.Parameters.Add("@Surname", OleDbType.VarChar).Value = TxtSurname.Text
            SQLCmd.Parameters.Add("@FirstNames", OleDbType.VarChar).Value = TxtFirstName.Text
            SQLCmd.Parameters.Add("@DateOfBirth", OleDbType.Date).Value = DatDOB.Value.Date
            SQLCmd.Parameters.Add("@PostCode", OleDbType.VarChar).Value = TxtPostCode.Text
            If CurrentStudentID = -1 Then
                SQLCmd.CommandText = "Insert into Student (Surname, FirstNames, DateOfBirth, PostCode) Values (@Surname, @FirstNames, @DateOfBirth, @PostCode)"
                cn.Open()
                SQLCmd.ExecuteNonQuery()
                SQLCmd.Parameters.Clear()
                SQLCmd.CommandText = "Select @@Identity"
                CurrentStudentID = CInt(SQLCmd.ExecuteScalar)
                LblStudentID.Text = CurrentStudentID
            Else
                SQLCmd.CommandText = "Update Student Set Surname = @Surname, FirstNames = @FirstNames, DateOfBirth = @DateOfBirth, PostCode = @PostCode, where StudentID = @CurrentStudentID"
                SQLCmd.Parameters.Add("@CurrentStudentID", OleDbType.Integer).Value = CurrentStudentID
                cn.Open()
                SQLCmd.ExecuteNonQuery()
            End If
        End Using
    End Using
End Sub