Home

Conditional Selection

 

Introduction

Instead of using all data as we have done so far using the SELECT keyword, you can present a condition that the database would follow to isolate specific records. One of the keywords you can use to formulate conditions is WHERE. Its basic syntax is:

SELECT What FROM WhatObject WHERE Expression;

The expressions used in conditions are built using algebraic, logical, and string operators. The Expression factor is called a criterion. Although a group of expressions, making it plural is called criteria, the word criteria is sometimes used for a singular expression also.

The expression is written using the formula:

ColumnName=Value

The ColumnName factor must be an existing column of the table. It is followed by the assignment operator. The Value factor is the value that would set the condition. If the value is a word or a group of words (also called a string), you must include it in single-quotes. Here is an example:

private void btnPeople_Click(object sender, EventArgs e)
{
    ADODB.RecordsetClass rstPeople = new ADODB.RecordsetClass();
    ADODB.ConnectionClass conADO = new ADODB.ConnectionClass();

    conADO.Open("Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source='C:\\Programs\\People.mdb'", "", "", 0);
    rstPeople.Open("SELECT LastName, FirstName, Gender " +
                   "FROM Persons " +
                   "WHERE Gender='Male';",
                   conADO,
                   ADODB.CursorTypeEnum.adOpenDynamic,
                   ADODB.LockTypeEnum.adLockOptimistic, 0);

    rstPeople.Close();
}

If it is a number, you can type its numeric value.

The TRUE and FALSE Constants

In Boolean algebra, something is considered TRUE when it holds a value. The value is also considered as 1 or Yes. By contrast, if something doesn't hold a value, it is considered non-existent and non-worthy of consideration. Such a thing has a value of FALSE, 0, or No. To retrieve such a value, you can just find out if the value of a field exists or not.

The comparison for a True or False value can be performed on a Boolean column. If a record has a value of 1, the table considers that such a field is True. If the field has a 0 value, then it holds a FALSE value.

The NULL Constant

While the True and False constants are used to find out whether a Boolean expression has a positive or a negative value, the database provides another constant used to find out whether a field is empty. This can be done using the NULL constant.

When a field holds a value, the value would be considered using the comparison operators we will learn shortly. If the field is empty, it holds a value of NULL.

 
 

Previous Copyright © 2005-2016, FunctionX Next