asp.net.ph

Skip Navigation LinksHome > Data Access / ADO.NET > Structured Query Language (SQL) Basics > Introduction to Queries

Introduction To Queries


Queries are used to retrieve a specific set of data from a data source, specifically from a database.

We generally use queries to be able to view, change, and analyze data in different ways.

NOTE: The material covered in this section assumes some familiarity with database fundamentals.

Queries are actually SQL commands, created using Structured Query Language ( SQL ) syntax.

ADO.NET provides the interface for accessing and manipulating data, while SQL provides the language, or means of communication, between ADO.NET and a data store.

Together, they provide a simple yet powerful means to easily develop engaging data-based applications.

Master/Detail Example Using Views
Run Sample View Source
Navigating thru the Rows of a DataTable
Run Sample | View Source
MultiView Navigation Using SetActiveView
Run Sample | View Source

In brief, SQL queries provide a potent medium to gather specific information we need.

With SQL, we can choose which data we want in the query results, define specific criteria that will give the results we want, and set ordering and/or grouping options to organize and analyze the query results.

In addition, SQL allows us to easily perform calculations on groups of records, such as to sum, average, count, and do other types of calculations for one or more chosen columns.

Moreover, SQL is largely used to update data ( add, edit or delete records ) and to create or modify table structures in a database.

This article explores the essentials of the language, but first, let’s look at the different types of queries in brief.

SELECT queries

The most common type of query is a SELECT statement that returns rows into a result set from one or more tables.

A SELECT query can contain specifications for the columns to return, the rows to select, the order to put the rows in, and how to group or summarize information.

A SELECT query does not change data in a table.

This sample demonstrates how you can use the simple power of a SELECT statement to fetch column values from a data source, and dynamically bind them to an ASP.NET control.

SQL SELECT Example 1
Run Sample | View Source

The above demonstrates using a SELECT query to retrieve ALL rows and columns from a data source.

Next let us look at a SELECT query that retrieves and displays only a specified data set, based on a variable ( a varying value ) selected by the user.

Dynamic SELECT queries

Following is a simple example of how to pass a variable to a SELECT query to specify which record to display. Click on the ProductCode link to see.

SQL SELECT Example 2
Run Sample | View Source

The above demonstrates using a SELECT query in two scenarios:

  • first, to display ALL rows and columns from the data source;
  • second, to display a specific record from the data source, given a value that is first passed to the Request.QueryString of the details page, which is then used to pass on to the SELECT statement.

Next let us look at another method of passing variables to SELECT queries.

Parameterized SELECT queries

A parameterized query is simply a query that is passed on one or more parameters, likewise the values we need to retrieve a specific set of records from a data source.

The difference is that SQL command parameters are declared as specific SQL data types ( SqlDbType ), such as varchar, int, datetime, bit, etc., which are internally converted to .NET Framework types ( String, Int32, DateTime, Boolean, etc. ).

This provides type checking and validation, which helps guard against “SQL injection” attacks, in which an attacker inserts a command into the Request.QueryString that can compromise security on the server.

In addition, parameters can pass values not only to SQL statements but to stored procedures as well.

Here is a basic example of how to declare and pass a parameter to an SQL SELECT command.

SQL SELECT Example 3
Run Sample | View Source

While we have thus far focused on SELECT queries, including dynamic select and parameterized select queries, SQL can be used to create the following query or command types, which we shall cover in later sections.

Action queries

An action query is a query that can make changes to multiple records in one operation. SQL command parameters are particularly useful in these kind of queries.

Soon we shall look at, and provide examples of, four types of action queries: INSERT, UPDATE, DELETE, and CREATE TABLE.

Extended SQL queries

An extended SQL query is a query for use in SQL-specific functions. Soon we shall look at, and provide examples of four SQL-specific queries: union, pass-through, data-definition, and subquery.

See Also

SQL Basic Concepts



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note