ASP.NET & Databases Part 1
page 3 of 7
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33344/ 57

Command


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.

objCmd.ExecuteNoQuery

                                    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.


View Entire Article

User Comments

Title: Re: One comment   
Name: Philip Q
Date: 2004-10-25 11:08:14 PM
Comment:
Jen,

The reason for that is that you have imported System.Data, but not System.Data.OleDb into your page.
Title: One comment.   
Name: Jen
Date: 2004-10-25 9:33:47 AM
Comment:
Putting the OleDBConnection command into the page_load event gives the error that OleDBConnection is not defined. However, OleDb.OleDbConnection works.
Title: Good...how to work with more than one dataset   
Name: Santhosh Kumar A.
Date: 2004-10-19 6:41:29 AM
Comment:
I found it very good. Please try to include how
to manipulate rows using For Each ... Next loop.
I was really searching for it.

Santhosh Kumar A.
Kerala, India

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 7:00:11 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search