Cω - Samples

Top Keyword Tutorial

This tutorial shows in a simple Cω program how to use the TOP keyword within a SQL Select expression.

Sample Files

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

These files are located in the \samples\SQL\Top subdirectory under the path where you installed Cω, which by default is C:\Program Files\Microsoft Research\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.

Further Reading

Example

The following is a complete Cω program that demonstrates how to use the TOP keyword within a SQL Select expression to limit the number of rows returned to the top ten rows in the results set.

using System;
using System.Data.SqlTypes;
using System.Query;
using Northwind;

public class Test {
  static void Main() {

    // use top to limit the number of rows returned
    foreach( row in select top 10 ContactName, Phone from DB.Customers order by ContactName ) {
      Console.WriteLine("Name: {0,-20} Phone#: {1,-12}", row.ContactName, row.Phone);
    }

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

Output

Name: Alejandra Camino     Phone#: (91) 745 6200
Name: Alexander Feuer      Phone#: 0342-023176
Name: Ana Trujillo         Phone#: (5) 555-4729
Name: Anabela Domingues    Phone#: (11) 555-2167
Name: André Fonseca        Phone#: (11) 555-9482
Name: Ann Devon            Phone#: (171) 555-0297
Name: Annette Roulet       Phone#: 61.77.61.10
Name: Antonio Moreno       Phone#: (5) 555-3932
Name: Aria Cruz            Phone#: (11) 555-9857
Name: Art Braunschweiger   Phone#: (307) 555-4680

Press ENTER to continue...