13.07.2015 Views

Using the Caché Managed Provider for .NET - InterSystems ...

Using the Caché Managed Provider for .NET - InterSystems ...

Using the Caché Managed Provider for .NET - InterSystems ...

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Using</strong> <strong>Caché</strong> Queries5.4 <strong>Using</strong> <strong>Caché</strong> QueriesA <strong>Caché</strong> Query is an SQL query defined as part of a <strong>Caché</strong> class. For example, <strong>the</strong> Sample.Personclass defines <strong>the</strong> ByName query as follows:Query ByName(name As %String = "") As %SQLQuery(CONTAINID = 1, SELECTMODE ="RUNTIME")[ SqlName = SP_Sample_By_Name, SqlProc ]{SELECT ID, Name, DOB, SSNFROM Sample.PersonWHERE (Name %STARTSWITH :name)ORDER BY Name}In <strong>the</strong> Sample.Person proxy class, ByName is a class method. It accepts a connection object, andreturns an ADO <strong>Provider</strong> CacheCommand object that can be used to execute <strong>the</strong> predefined SQL query:CacheCommand Command = Sample.Person.ByName(CacheConnect);In this example, <strong>the</strong> Command.Connection property has been set to CacheConnect, andCommand.CommandText contains <strong>the</strong> predefined ByName query string. To set <strong>the</strong> Command.Parametersproperty, we create and add a CacheParameter object with a value of A (which will get all recordswhere <strong>the</strong> Name field starts with A):CacheParameter Name_param = new CacheParameter("name", CacheDbType.NVarChar);Name_param.Value = "A";Command.Parameters.Add(Name_param);The CacheParameter and CacheDataReader ADO <strong>Provider</strong> classes must be used to define parametersand execute <strong>the</strong> query, just as <strong>the</strong>y are in an ADO SQL query (see <strong>Using</strong> SQL Queries with CacheParameter).However, this example will use <strong>the</strong> query results to access a set of proxy objects.A CacheDataReader object is used to get <strong>the</strong> ID of each row in <strong>the</strong> result set. Each ID is used toinstantiate <strong>the</strong> corresponding Sample.Person proxy object, which is <strong>the</strong>n used to access <strong>the</strong> data:Sample.Person person;string ID;CacheDataReader reader = Command.ExecuteReader();while (reader.Read()) {ID = reader[reader.GetOrdinal("ID")].ToString();person = Sample.Person.OpenId(CacheConnect, ID);Display.WriteLine(person.Id() + "\t"+ person.Name + "\n\t"+ person.SSN + "\t"+ person.DOB.ToString().Split(' ')[0].ToString());};<strong>Using</strong> <strong>the</strong> <strong>Caché</strong> <strong>Managed</strong> <strong>Provider</strong> <strong>for</strong> .<strong>NET</strong> 39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!