Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.

For information on migrating to InterSystems IRIS, see How to Migrate to InterSystems IRIS, available on the WRC Distributions page (login required).

A Query with Parameters

To execute a more complex query that uses input parameters, you must use CacheParameter to define the parameters. The following method displays all of the contacts of a particular type. The type is passed to the method at runtime.


public void DisplayContacts(string type)
{
 try{
   string sql = "Select %ID, Name, ContactType From Provider.Contact "+
                                "Where ContactType %startswith ? Order By Name";
   
   CacheParameter type_param = new CacheParameter();
   type_param.CacheDbType = CacheDbType.NVarChar;
   type_param.Direction = ParameterDirection.Input;
   type_param.Size = 50;
   type_param.ParameterName="ContactType";
   type_param.Value = type;
   
   CacheCommand command = new CacheCommand(sql, cnCache);
   command.Parameters.Add(type_param);
   CacheDataReader reader = command.ExecuteReader();
   while (reader.Read())
   {
     Console.WriteLine("ID: {0} Name: {1}", reader[reader.GetOrdinal("ID")], 
                                             reader[reader.GetOrdinal("Name")]);
   }
 }
 catch (CacheException e){}
}
 

Note that cnCache represents an open CacheConnection instance.

Feedback