ado02Example_main.cs

TBD: Update GenDefines.txt with a $DESCRIPTION for this example.

using System;
/* Raima.Rdm is the system module that contains the ADO.NET
* data provider for RDM */
using Raima.Rdm;
/* EXAMPLE - SQL02 for C# using ADO.NET */
/* When you run this example, a new database is created with two tables
* where one references the other. Then a single row is created in the
* referenced table and four rows are created in the referencing table.
* Finally all the data is read from the database using a join.
*
* The following methods are illustrated:
* Raima.Rdm.RdmConnection:
* Close() Method
* CreateCommand() Method
* Open() Method
* RdmConnection() Constructor
* Raima.Rdm.RdmCommand:
* CommandText Property
* ExecuteNonQuery() Method
* ExecuteReader() Method
* Raima.Rdm.RdmDataReader:
* Item[int] Property
* Read() Method
*
* They are fully document in http://docs.raima.com/rdm/.
*
* For simplicity this example does not catch any thrown execptions
* but good programming practices would dictate that this occur in
* a full application.
*/
namespace SQL02
{
class Program
{
static void CreateDatabase()
{
/* Attempt to drop any existing database. If the database does not
* already exists this will fail and we simply ignore the failure
* for this sample. */
RdmConnection conn = new RdmConnection("host=local");
conn.Open();
RdmCommand cmd = conn.CreateCommand();
try
{
string[] dropCmds =
{
"DROP DATABASE SQL02",
"COMMIT"
};
foreach (string c in dropCmds)
{
cmd.CommandText = c;
cmd.ExecuteNonQuery();
}
}
catch (Exception)
{
}
/* Attempt to create the database */
string[] createCmds =
{
"CREATE DATABASE SQL02",
"CREATE TABLE PriTab (pid INTEGER PRIMARY KEY, pstr CHAR(30))",
"CREATE TABLE ForTab (fstr CHAR(30), pid INTEGER REFERENCES PriTab(pid))",
"COMMIT"
};
foreach (string c in createCmds)
{
cmd.CommandText = c;
cmd.ExecuteNonQuery();
}
conn.Close();
}
static void Main(string[] args)
{
CreateDatabase();
/* Create the connection with a simple connection string */
RdmConnection conn = new RdmConnection("host=local;database=SQL02");
/* Open the connection to the database */
conn.Open();
/* Create a new command */
RdmCommand cmd = conn.CreateCommand();
/* Insert a row into the referenced table */
cmd.CommandText = "INSERT INTO PriTab VALUES 1, 'referenced 1'";
cmd.ExecuteNonQuery();
/* Insert four rows into the referencing table */
for (int ii = 1; ii < 5; ii++)
{
cmd.CommandText = "INSERT INTO ForTab VALUES 'referencing " + ii + "', 1";
cmd.ExecuteNonQuery();
}
/* Scan through all the existing rows using a join and print out
* the contents */
cmd.CommandText = "SELECT pstr, fstr FROM ForTab, PriTab where ForTab.pid = PriTab.pid";
RdmDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader[0] + " -- " + reader[1]);
conn.Close();
}
}
}