谁能帮我通过Visual Basic更新Microsoft访问记录?

问题描述 投票:-1回答:1

目前,我有一个代码段,让我来搜索信息,并添加到Microsoft Access文件信息。当我尝试更新的信息中出现的问题,它通过else语句,但无助于访问文件。是不是有什么毛病我的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
vb.net ms-access
1个回答
0
投票

请打开选项严格。您可以在工具菜单此设置 - >选项 - >项目和解决方案 - > VB的默认值。这将节省你的错误在运行时。

使用参数与访问最重要的是,当他们出现在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
© www.soinside.com 2019 - 2024. All rights reserved.