Skip to main content

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.

FeedbackOpens in a new tab