ado09Example_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 - SQL09 for C# using ADO.NET */
/* This example shows how to use transactions with SQL commands. A database
* is created and then data is inserted within a transaction, which is
* committed. Another transaction is also started and more data is inserted
* but this time the transaction is aborted. The data is then read out and
* displayed showing that only the data in the first transaction is in the
* database
*
* The following methods are illustrated:
* Raima.Rdm.RdmConnection:
* BeginTransaction() Method
* 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
* Raima.Rdm.RdmTransaction
* Commit() Method
* Rollback() 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 SQL09
{
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[] cmds =
{
"CREATE TABLE Table1 (id INTEGER PRIMARY KEY)"
};
/* Drop any existing copy of the database */
DropDatabase(conn, "SQL09");
/* Attempt to create the database */
CreateThisDatabase(conn, "SQL09", cmds);
}
static void Main(string[] args)
{
/* Create the connection with a simple connection string */
RdmConnection conn = new RdmConnection("host=local");
conn.Open();
CreateDatabases(conn);
/* Open the database */
conn.ChangeDatabase("SQL09");
/* Start a new transacation */
RdmTransaction trans = conn.BeginTransaction();
/* Create a new command */
RdmCommand cmd = conn.CreateCommand();
/* Insert three rows into the table */
for (int ii = 1; ii < 4; ii++)
{
cmd.CommandText = "INSERT INTO Table1 VALUES " + ii;
cmd.ExecuteNonQuery();
}
/* Commit the transaction */
trans.Commit();
/* Start a new transacation */
trans = conn.BeginTransaction();
/* Insert three rows into the table */
for (int ii = 4; ii < 7; ii++)
{
cmd.CommandText = "INSERT INTO Table1 VALUES " + ii;
cmd.ExecuteNonQuery();
}
/* Rollback the transaction */
trans.Rollback();
/* Scan through the data, displaying the rows */
cmd.CommandText = "SELECT id FROM Table1";
RdmDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader[0]);
conn.Close();
}
}
}