This tutorial shows in a simple Cω program how to returns a single row as the result of a SQL Select transaction.
To run this tutorial, you may use the following project and source files:
These files are located in the \samples\SQL\Singleton 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.
When you write code in Cω for a SQL transaction that retrieves a single row of results, this is known as a singleton Select expression.
The following is a complete Cω program that demonstrates how to perform a singleton Select transaction.
using System;
using System.Data.SqlTypes;
using System.Query;
using Northwind;
public class Test {
static void Main(string[] args) {
string cid = args.Length > 0 ? args[0] : "ALFKI";
try {
row = select singleton ContactName, Phone from DB.Customers where CustomerID == cid;
Console.WriteLine("Customer for ID: {0}", cid);
Console.WriteLine("Name: {0} Phone#: {1}", row.ContactName, row.Phone);
}
catch(StreamNotSingletonException e) {
Console.WriteLine("Cannot find customer matching id: {0}", cid);
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
Customer for ID: ALFKI Name: Maria Anders Phone#: 030-0074321 Press ENTER to continue...