04-25-2011, 11:32 AM
Here this should help you get started. Since I don't know the schema of your database, I created a sample database. I made a sample project that demonstrates adding Values to a database table and reading from the same table. The code can easily be modified to suit your needs and to add to/read from multiple tables. If you need any help, respond and someone will help you out.
Click here to download the Sample Project and database.
Code:
Imports System.Data.OleDb
Public Class Form1
Dim SQL As String 'holds the SQL string sent to the database
Dim dbCommand As OleDbCommand 'sends the SQL string
Dim dbConnection As OleDbConnection 'connects us to the database
Dim dbFilePath As String = "C:\Users\KoBE\Documents\dbTest.accdb" 'file where database is located
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
'create new connection
dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
'the sql (instructions)
SQL = "INSERT INTO tblTest (fldTest) VALUES ('" & TextBox1.Text & "')"
'connect to the database
dbConnection.Open()
'create new command to send the sql through the connection we just initiated
dbCommand = New OleDbCommand(SQL, dbConnection)
'send the command
dbCommand.ExecuteNonQuery()
'close the connection
dbConnection.Close()
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
'sql command
SQL = "SELECT fldTest FROM tblTest"
'new connection
dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
'new command to send the sql
dbCommand = New OleDbCommand(SQL, dbConnection)
'data will be read via this DataAdapter
Dim data As New OleDbDataAdapter(dbCommand)
'creates a new data table that will store the data in memory
Dim table As New DataTable("Test")
'open the connection
dbConnection.Open()
'fill the table with the data
data.Fill(table)
'for each row show the value in the field 'fldTest'
For Each row As DataRow In table.Rows
MessageBox.Show(row.Item("fldTest"))
Next
End Sub
End Class
Click here to download the Sample Project and database.
I code at http://tech.reboot.pro