How to execute simple Database Queries with VB .NET
The SqlCommand class in the .NET Framework Data Provider has four methods that you can use to execute SQL statements:
- ExecuteScalar: Executes a query that returns a single scalar value.
- ExecuteReader: Executes a query that returns a result set.
- ExecuteNonQuery: Executes a data update statements or a catalog update statement.
- ExecuteXmlReader: Executes a query that returns an Extensible Markup Language (XML) result set, this method is only avaliable in the SqlCommand class.
To execute a simple database query
Import the System.Configuration namespace
Use the ConfigurationManager.ConnectionStrings property to get a collection of connection strings from the application configuration file.
Index into the collection of connection strings by using the programmatic name of the connection string you want to access.
- Use the ConnectionString property to get the connection string information.
- Create a connection object.
- Create a command object.
If you want to execute an SQL statement, set the CommandType property of the command object to the CommandType.Text enumeration value. If you want to call a stored procedure, set the CommandType property of the command object to the CommandType.StoredProcedure enumeration value.
- Call the Open method on the connection object.
- Call the ExecuteScalar method on the command object. Assign the result to a suitably typed variable.
- Call the Close method on the connection object.
The following example shows how to execute a group of queries with one aggregate function to perform a calculation on a set of values from the AdventureWorks database on the local SQL Server instance and return a single value.
Fig 1. Main program, running the first query
Fig 2. Running the second query
Fig 3. Running the third query
Fig 4. Running the fourth query
Fig 5. Running the last query