ado06Example_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 - SQL06 for C# using ADO.NET */
/* When you run this example, two databases are created each with a table.
* Each table is populated with a few rows and finally the data read with
* a single query.
*
* The following methods are illustrated:
* Raima.Rdm.RdmConnection:
* ChangeDatabase(string) Method
* 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 SQL06
{
class Program
{
static void DropDatabase(
RdmConnection conn,
string name)
{
RdmCommand cmd = conn.CreateCommand();
/* Attempt to drop this database. If the database does not
* already exist this will fail and we simply ignore the
* failure for this sample. */
try
{
cmd.CommandText = "DROP DATABASE " + name;
cmd.ExecuteNonQuery();
cmd.CommandText = "COMMIT";
cmd.ExecuteNonQuery();
}
catch (Exception)
{
}
}
static void CreateThisDatabase(
RdmConnection conn,
string name,
string[] cmds)
{
RdmCommand cmd = conn.CreateCommand();
/* Create the specified database. Make sure the database is
* closed when we are done so other databases can be created
* after this if necessary. */
cmd.CommandText = "CREATE DATABASE " + name;
cmd.ExecuteNonQuery();
foreach (string c in cmds)
{
cmd.CommandText = c;
cmd.ExecuteNonQuery();
}
cmd.CommandText = "COMMIT";
cmd.ExecuteNonQuery();
cmd.CommandText = "CLOSE DATABASE " + name;
cmd.ExecuteNonQuery();
}
static void CreateDatabases(
RdmConnection conn)
{
string[] cmds1 =
{
"CREATE TABLE Table1 (id INTEGER PRIMARY KEY, str CHAR(30))"
};
string[] cmds2 =
{
"CREATE TABLE Table2 (id INTEGER, str CHAR(30))"
};
/* Drop any existing copies of the database */
DropDatabase(conn, "SQL06_1");
DropDatabase(conn, "SQL06_2");
/* Attempt to create the databases */
CreateThisDatabase(conn, "SQL06_1", cmds1);
CreateThisDatabase(conn, "SQL06_2", cmds2);
}
static void Main(string[] args)
{
/* Create the connection with a simple connection string */
RdmConnection conn = new RdmConnection("host=local");
conn.Open();
CreateDatabases(conn);
/* Create a new command */
RdmCommand cmd = conn.CreateCommand();
/* Specify which databases to use */
cmd.CommandText = "USE SQL06_1";
cmd.ExecuteNonQuery();
cmd.CommandText = "USE SQL06_2";
cmd.ExecuteNonQuery();
/* Insert one row into the table in the first database */
cmd.CommandText = "INSERT INTO Table1 VALUES 1, 'database1 1'";
cmd.ExecuteNonQuery();
/* Insert three rows into the table in the second database */
for (int ii = 1; ii < 4; ii++)
{
cmd.CommandText = "INSERT INTO Table2 VALUES 1, 'database2 " + ii + "'";
cmd.ExecuteNonQuery();
}
/* Scan through the data, generating the result */
cmd.CommandText = "SELECT Table1.str, Table2.str FROM Table1, Table2 WHERE Table1.id = Table2.id";
RdmDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader[0] + " -- " + reader[1]);
conn.Close();
}
}
}