3
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by hirenpatel53
0

Here how to retrieve data:

Private Sub GetData(ID As Integer)
   Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;")
   Dim com As OleDbCommand = Nothing
   Dim dr As OleDbDataReader = Nothing

   Try
      con.Open()
      com = New OleDbCommand("SELECT * FROM table WHERE id = " & ID, con)
      dr = com.ExecuteReader(CommandBehavior.CloseConnection)
      If dr.HasRows Then
         dr.Read()
         'assign data to labels and textboxes
         someTextBox.Text = dr.Item("field1")
      End If
      dr.Close()
   Catch ex As Exception
      If con.State = ConnectionState.Open Then
         con.Close()
      End If
   End Try
End Sub

Here's how to update the database:

Private Sub UpdateData(ID As Integer)
   Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;")
   Dim com As OleDbCommand = Nothing

   Try
      con.Open()
      com = New OleDbCommand("UPDATE table SET field1 = '" & someTextBox.Text & "' WHERE id = " & ID, con)
      com.ExecuteNonQuery()
      con.Close()
   Catch ex As Exception
      If con.State = ConnectionState.Open Then
         con.Close()
      End If
   End Try
End Sub

And finally, here's how to delete from the database:

Private Sub DeleteData(ID As Integer)
   Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;")
   Dim com As OleDbCommand = Nothing

   Try
      con.Open()
      com = New OleDbCommand("DELETE FROM table WHERE id = " & ID, con)
      com.ExecuteNonQuery()
      con.Close()
   Catch ex As Exception
      If con.State = ConnectionState.Open Then
         con.Close()
      End If
   End Try
End Sub

That's all there is to it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.