Basic ADO .NET Application Steps

An ADO .NET C# application program consists of a set of calls to the ADO .NET API functions in a particular sequence as outlined below. The database schema for this sample is helloWorld.sdl.

  1. Set up and initialize your application's use of the ADO .NET API as follows.
    1. Create a String object to define your connection string in the following format: "host=localhost;database=HelloWorldDb"
    2. Create an RdmConnection object to handle your connection to the Raima database system and pass in your connection string to the constructor.
    3. Create an RdmCommand object to handle your SQL commands you wish to use to interact with the RDM database system.
    4. Create an RdmTransaction object to handle your transactions within the RDM database system.
    5. Create an RdmDataReader object to handle reading out data from the RDM database system.
    6. Call the Open function of the RdmConnection object in order to open your database.
  2. Prepare your application to execute SQL statements as follows.
    1. Set the CommandText property of the RdmCommand object to define the SQL statement that will need to be executed by your application.
    2. Create some RdmParameter objects for any parameters you will need for any parameter markers that were specified in the SQL statements defined in the prior step.
    3. Call the Parameters.Add(param) to bind your application's variables to any parameters created in step b.
  3. At this point your application is execution ready. That means that your application will …
    1. Call ExecuteNonQuery on the RdmCommand object to execute the appropriate statements that implement the database access needs for each particular function. Alternatively you can call ExecuteScalar to execute a statement and return the first column of the first row of results.
    2. Call transaction statements using the RdmTransaction class in order to start the transaction, commit the transaction, create a savepoint, or rollback a transaction.
    3. Call ExecuteReader to retrieve the result rows from an executed select statement. You iterate over the results using the RdmDataReader object.
  4. When your application is ready to terminate you need to…
    1. Call Close on the RdmDataReader object you used if you read from one.
    2. Call Close on the RdmConnection object to close the connection to your database.

Hello World!

If you follow the basic procedure listed above you can easily create a simple database where you insert some data and the retrieve the data to be displayed back out. This can be seen with the common Hello World example.

In the example below return codes are mostly ignored for simplicity. This will be OK because we know what the expected results are.

/***********************************************************************    HELLO WORLD ADO.NET**    ---------------**    This document describes the process to open a simple database, insert a record **    containing a text field, read the text field from database and print it out.**********************************************************************/using System;using Raima.Rdm;   /* The RDM ADO.NET API */using System.Data;namespace HelloWorldApplication{    class HelloWorld    {        static void Main()        {            // Setup connection and SQL query strings            string connectionString = "host=local;database=hello_worldADO";            string deleteString = "DELETE FROM hello_table";            string insertString = "INSERT INTO hello_table(f00) VALUES(?)";            string queryString = "SELECT * FROM hello_table";            // Create connection object to connect to the database            RdmConnection connection = new RdmConnection(connectionString);            // Create a command object to execute SQL statements            RdmCommand command = connection.CreateCommand();            // Create a transaction object to start and stop transactions            RdmTransaction rdmtrans = null;            // Create a reader object to read out data from the SQL query            RdmDataReader reader = null;            try            {                /* Open a connection to the database */                connection.Open();                // Start a transaction                rdmtrans = connection.BeginTransaction();                command.CommandText = deleteString;                // Execute the DELETE statement                command.ExecuteNonQuery();                // Execute the INSERT statement                command.CommandText = insertString;                // Set up the input parameter                RdmParameter parameter = new RdmParameter();                parameter.RdmType = RdmType.AnsiString;                parameter.Direction = ParameterDirection.Input;                parameter.Value = "Hello World!";                command.Parameters.Add(parameter);                // Execute the INSERT statement                command.ExecuteNonQuery();                command.Parameters.Clear();                // Commit the transaction                rdmtrans.Commit();                command.CommandText = queryString;                // Execute a select statement which should return results                reader = command.ExecuteReader();                // Read all of the results                while (reader.Read())                {                    // Print out each row of the select in a newline                    Console.WriteLine(reader.GetString(0));                }                // Close the reader since retrieved all rows                reader.Close();                // Close the connection                connection.Close();            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }        }    }}