Cω - Samples

Insert and Delete Tutorial

This tutorial shows in a simple Cω program the use of SQL the use of SQL insert and delete expressions.

Sample Files

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

These files are located in the \samples\SQL\Insert 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 as provided by Cω to you after installation:

<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 the use of SQL insert and delete expressions.

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

/// <summary>
/// This sample illustrates the use of SQL insert and delete expressions.
///
/// To run this sample, check the Insert.exe.config file to make sure the Northwind
/// database connection string is correct, then hit F5
/// </summary>

public class Test {
  public static void Main( string[] args ) {
    int myid = (args.Length > 0) ? Int32.Parse(args[0]) : 123;

    try {
      // insert is an expression that results in the number of rows successfully inserted
      int i = insert [RegionID=myid, RegionDescription="test-region"] into DB.Region;
      Console.WriteLine("{0} row has been inserted", i);
    }
    catch {}

      try {
        // delete is an expression that results in the number of rows successfully deleted
      i = delete from DB.Region where RegionID == myid;
      Console.WriteLine("{0} row has been deleted", i);
    }
    catch {}

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

Output

1 row has been inserted
1 row has been deleted

Press ENTER to continue...