Using a DataSet
The following code creates a CacheDataAdapter object and uses it to fill a DataSet with the MVFILE.PERSON data. In more detail, the code does the following:
-
Creates a connection to Caché using the CMP CacheConnection class. Note that when using a DataSet the connection object does not need to be explicitly opened or closed. The DataSet manages the connection state.
-
Prepare a CacheDataAdapter object. In this case we supply the CacheDataAdapter object with both a CacheConnection object and SQL SELECT text. We can also supply INSERT, UPDATE, and DELETE comands to the adapter if it is to support these operations. Provide these commands to the adapter in the form of properly configured CacheCommand objects.
-
Use the CacheDataAdapter Fill to fill the data set with MVFILE.PERSON data.
-
Display the data in the DataSet.
//Create the connection
string cacheString =
"Server=localhost;Port=1972; Log File=c:\\MVTutorial.log;Namespace=MYACCOUNT;"
+ "Password=SYS;User ID=_system;";
cnCache = new CacheConnection(cacheString);
//Prepare the CacheDataAdapter and the DataSet
string select = "SELECT * FROM MVFILE.PERSON";
CacheDataAdapter dataAdapter = new CacheDataAdapter(select, cnCache);
DataSet dataSet = new DataSet();
//Fill the DataSet using the CacheDataAdapter
dataAdapter.Fill(dataSet,"PERSON");
//Display the DataSet data
foreach (DataRow dr in dataSet.Tables["PERSON"].Rows)
{
Console.WriteLine("ID: {0} NAME: {1} AGE: {2} HAIR: {3} PHONES: {4}",
dr["ItemID"],dr["NAME"], dr["AGE"], dr["HAIR"], dr["PHONE"]);
}
The above code is contained in RelationalAccess.cs. The file is in <cachesys>\Dev\tutorials\mv. Executing the .NET Examples contains step-by-step instructions for executing this code.