Command
Once you've got a
connection to the database, you can then send commands to it. The OleDbCommand
object allows you to send commands to the database. You enter a SQL statement
and depending on how you execute it (it has several execute commands) it can
do almost anything with the data.
Dim
objCmd as New OleDbCommand("SELECT * From users", objConn) |
As you can see, its
simple, it takes in a SQL String and the name of a connection object to use.
You can also do it this way -
Dim
objCmd as New OleDbCommand()
'Later on
objCmd.Connection = objConn
objCmd.CommandText = "SELECT * FROM users"
'You
could even do it like this -
Dim objCmd as New OleDbCommand(SQL String, connection string) |
We won't worry about
these, they do exactly the same thing with the last one you provide a
connection string instead of a connection object.
At this stage, we haven't executed the statement yet and there are numerous
ways to do this.
·
ExecuteNonQuery
This is the way you execute when the string
isn't going to return any data, like an INSERT or UPDATE SQL string.
It does its job and that's it.
·
ExecuteReader
If you've got a data reader (explained later,
then you can use this to put the data into a data reader -
Dim
objRd as OleDbDataReader
objRd = objCmd.ExeuteReader |
·
ExecuteScalar
Use the ExecuteScalar method to retrieve a
single value (for example, an aggregate value) from a database.
I've been using the OleDbCommand to insert things into a database, it's quite
easy actually.