简体   繁体   中英

How can I Update access database from vb.net app

How can I update my access database in vb.net code I use OleDb connection to the database it's make change in datagridview but not into the database

    Public Sub executquery()
        Dim commandOleDb As New OleDbCommand(query, con)
        commandOleDb.ExecuteNonQuery()
        con.Close()
    End Sub
--------------------
    Private Sub ButtonInsert_Click(sender As Object, e As EventArgs)
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Almaashat.accdb")
        Dim query As String
        Try
            con.Open()
            query = "INSERT INTO Techers (File_ID,Name,Workplace,Jop,Appointment,Class,Birthday,End_date,End_class,End_for,Note) VALUES (" & TextBoxFile_ID.Text & " ,'" & TextBoxName.Text & "' ,'" & TextBoxWorkplace.Text & "' ,'" & TextBoxJop.Text & "'  ,'" & DateTimePickerAppoiment.Text & "','" & TextBoxClass.Text & "','" & DateTimePickerBirthday.Text & "' ,'" & DateTimePickerEnd_date.Text & "' ,'" & TextBoxEnd_class.Text & "','" & TextBoxEnd_for.Text & "' ,'" & TextBoxNote.Text & "')"
            executquery()
            con.Close()
            MsgBox("Your Data Inserted")
        Catch ex As Exception
            MsgBox("Your Data Not Inserted")
        End Try
        TechersDataGridView.DataSource = TechersBindingSource
    End Sub

You have the definition of string 'query' and OleDBConnection 'con' only in your private Sub executquery

So to correct that :

Public Sub executquery(query as String, con as OleDBConnection)

        Dim commandOleDb As New OleDbCommand(query, con)
        con.Open()
        commandOleDb.ExecuteNonQuery()
        con.Close()
End Sub

 Private Sub ButtonInsert_Click(sender As Object, e As EventArgs)
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Almaashat.accdb")
    Dim query As String
    Try
       query = "INSERT INTO Techers (File_ID,Name,Workplace,Jop,Appointment,Class,Birthday,End_date,End_class,End_for,Note) VALUES (" & TextBoxFile_ID.Text & " ,'" & TextBoxName.Text & "' ,'" & TextBoxWorkplace.Text & "' ,'" & TextBoxJop.Text & "'  ,'" & DateTimePickerAppoiment.Text & "','" & TextBoxClass.Text & "','" & DateTimePickerBirthday.Text & "' ,'" & DateTimePickerEnd_date.Text & "' ,'" & TextBoxEnd_class.Text & "','" & TextBoxEnd_for.Text & "' ,'" & TextBoxNote.Text & "')"
        executquery(query,con)
        MsgBox("Your Data Inserted")
    Catch ex As Exception
        MsgBox("Your Data Not Inserted")
    End Try
    TechersDataGridView.DataSource = TechersBindingSource
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM