|
Explanation
The database and the data set are not connected. A connection
object is used to connect them, then the Data Adapter is used
communicate between them.
The Connection Object
This connects the database. The one used to connect to an
Access database is Jet OLE DB. Others are SQL Server OLE DB another is Oracle OLE DB.
The following statement declares con as a variable to give the
connection object the information it needs. Declare this
variable with the other public variables.
Dim con As New
OleDb.OleDbConnection
The connection object needs
to know what type of data base connection to use and what the database
file to use. The connection string is set to work with an Access
database with the following statement t.
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;data
source=C:\folder\filename.mdb"
The Data Adapter
The Data Adapter communicates between the Connection Object to the
Data Set. It will need to know two things - what to read or write to
the database and what is the connection to use
Declare the Data Adapter like this with the other dimension
statements.
Dim da As
OleDb.OleDbDataAdapter
When you ask for a read or write action to the data set, you pass
the information to the data adapter like this:
da = New
OleDb.OleDbDataAdapter(sql, con)
The Data Set
You have one data set in the project, which is directly from your
database, but you'll remember that one can't accept any
modifications.. You will need a second one that can be modified.
Declare the dataset like this with the other dimension statements.
Dim ds As
New DataSet
The Query String
This is the command used to read from or write to a database. It
will be passed using a string variable. Declare the query string
variable like this with the other dimension statements. The sql
command in this case draws all records from a table in an Access
database.
Dim sql As String
Assign the a command to the variable like this:
sql = "SELECT * FROM TableName"
The * means all records.
|