Cω - Samples

Select Tutorial

This tutorial shows in a simple Cω program how to use SQL Select to select data from the Customers table in the Northwind sample database provided with SQL Server 2000 and iterate over the results.

Sample Files

To run this tutorial, you may use the following project and source files:

These files are located in the \samples\SQL\Select subdirectory under the path where you installed Cω, which by default is C:\Program Files\Comega.

To run this sample, you need to have a SQL configuration file in the same directory as the Select.exe you make in Visual Studio. Here are the contents of the version of this file that Cω provided you as a starting point:

<configuration>
  <appSettings>
    <add key="Northwind" value="Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI" />
  </appSettings>
</configuration>

Verify that this is the correct SQL connection string for connecting to the Northwind database on your SQL Server 2000 installation. If necessary, you can modify the value attribute in the <add> element

When you have verified it is correct, press F5 in Visual Studio to make and run the sample.

Example

The following is a complete Cω program that demonstrates how to use SQL Select to select data from the Customers table in the Northwind sample database provided with SQL Server 2000 and iterate over the results.

using System;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Northwind;

public class Test {
  public static void Main( string[] args ) {
    string mycity = args.Length>0 ? args[0] : "London";

    Console.WriteLine("Contacts in \"{0}\"", mycity);
    Console.WriteLine();
    Console.WriteLine("CustomerID   Contact");
    Console.WriteLine("--------------------------------------------");

    // Select expressions return a stream of rows.  The type of each row
    // is determined by the projection list.
    struct{SqlString CustomerID; SqlString ContactName;}* res
      = select CustomerID, ContactName
            from DB.Customers
            where City == mycity
            order by ContactName;

    // The foreach statement can infer the type of the iteration variable 'row'
    // by looking at the strongly typed stream 'res.'
    foreach( row in res ) {
      Console.WriteLine("{0,-12} {1}", row.CustomerID, row.ContactName);
    }

    // You may also omit the type information on the left-hand side of an assignment
    // statement, so there is no need to respecify a type that is already known.
    res2 = select CustomerID, ContactName
            from DB.Customers
            where City == mycity
            order by ContactName desc;

    Console.WriteLine("\nIn reverse");
    foreach( row in res2 ) {
      Console.WriteLine("{0,-12} {1}", row.CustomerID, row.ContactName);
    }

    // Since its just an expression, you may also place the select expression directly
    // inside the foreach statement.
    Console.WriteLine("\nBy ID");
    foreach( row in select CustomerID, ContactName
                     from DB.Customers
                     where City == mycity
                     order by CustomerID ) {
      Console.WriteLine("{0,-12} {1}", row.CustomerID, row.ContactName);
    }

    Console.Write("\nPress ENTER to continue...");
    Console.ReadLine();
  }
}

Output

Contacts in "London"

CustomerID   Contact
--------------------------------------------
EASTC        Ann Devon
CONSH        Elizabeth Brown
SEVES        Hari Kumar
NORTS        Simon Crowther
AROUT        Thomas Hardy
BSBEV        Victoria Ashworth

In reverse
BSBEV        Victoria Ashworth
AROUT        Thomas Hardy
NORTS        Simon Crowther
SEVES        Hari Kumar
CONSH        Elizabeth Brown
EASTC        Ann Devon

By ID
AROUT        Thomas Hardy
BSBEV        Victoria Ashworth
CONSH        Elizabeth Brown
EASTC        Ann Devon
NORTS        Simon Crowther
SEVES        Hari Kumar

Press ENTER to continue...