ado04Example_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 - SQL04 for C# using ADO.NET */
/* When you run this example, a new database is created with a single table
* that has an index. Ten rows are inserted in random order.
* Finally all the data is read from the database using the index to order
* the results.
*
* 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 SQL04
{
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 SQL04",
"COMMIT"
};
foreach (string c in dropCmds)
{
cmd.CommandText = c;
cmd.ExecuteNonQuery();
}
}
catch (Exception)
{
}
/* Attempt to create the database */
string[] createCmds =
{
"CREATE DATABASE SQL04",
"CREATE TABLE IndTab (id INTEGER PRIMARY KEY)",
"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=SQL04");
/* Open the connection to the database */
conn.Open();
/* Create a new command */
RdmCommand cmd = conn.CreateCommand();
Random random = new Random(1);
/* Insert ten rows into the table with random data */
for (int ii = 0; ii < 10; ii++)
{
cmd.CommandText = "INSERT INTO IndTab VALUES " + (random.Next() % 100);
cmd.ExecuteNonQuery();
}
/* Scan through all the existing rows using the index for order */
cmd.CommandText = "SELECT id FROM IndTab order by id";
RdmDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader[0]);
conn.Close();
Console.WriteLine("Hit <Enter> to continue");
Console.ReadLine();
}
}
}