Products Support Documentation Download
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 helloWorldADO.sdl.

Set up and initialize your application's use of the ADO .NET API as follows.

  • Create the database using the helloWorldADO.sdl schema if it does not already exist.
    1. rdm-create helloWorldADO.sdl
  • Create a String object to define your connection string in the following format: "host=localhost;database=HelloWorldADO"
    1. Create an RdmConnection object to handle your connection to the Raima database system and pass in your connection string to the constructor.
    2. Create an RdmCommand object to handle your SQL commands you wish to use to interact with the RDM database system.
    3. Create an RdmTransaction object to handle your transactions within the RDM database system.
    4. Create an RdmDataReader object to handle reading out data from the RDM database system.
    5. Call the Open function of the RdmConnection object in order to open your database.
  • 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".
  • 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.
  • 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_worldADO.sdl
1 --
2 -- Use following command to compile:
3 -- rdm-create hello_worldADO.sdl
4 --
5 -- Please refer to the Database Design section in the User Manual for details of
6 -- RDM Database schema.
7 
8 create table hello_table
9 (
10  f00 char(31)
11 );

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.

1 /*********************************************************************
2 ** HELLO WORLD ADO.NET
3 ** ---------------
4 ** This document describes the process to open a simple database, insert a record
5 ** containing a text field, read the text field from database and print it out.
6 **********************************************************************/
7 
8 using System;
9 using Raima.Rdm; /* The RDM ADO.NET API */
10 using System.Data;
11 
12 namespace HelloWorldApplication
13 {
14  class HelloWorld
15  {
16  static void Main()
17  {
18  // Setup connection and SQL query strings
19  string connectionString = "host=localhost;database=hello_worldADO";
20  string deleteString = "DELETE FROM hello_table";
21  string insertString = "INSERT INTO hello_table(f00) VALUES(?)";
22  string queryString = "SELECT * FROM hello_table";
23 
24  // Create connection object to connect to the database
25  RdmConnection connection = new RdmConnection(connectionString);
26 
27  // Create a command object to execute SQL statements
28  RdmCommand command = connection.CreateCommand();
29 
30  // Create a transaction object to start and stop transactions
31  RdmTransaction rdmtrans = null;
32 
33  // Create a reader object to read out data from the SQL query
34  RdmDataReader reader = null;
35 
36  try
37  {
38  /* Open a connection to the database */
39  connection.Open();
40 
41  // Start a transaction
42  rdmtrans = connection.BeginTransaction();
43 
44  command.CommandText = deleteString;
45 
46  // Execute the DELETE statement
47  command.ExecuteNonQuery();
48 
49  // Execute the INSERT statement
50  command.CommandText = insertString;
51 
52  // Set up the input parameter
53  RdmParameter parameter = new RdmParameter();
54  parameter.RdmType = RdmType.AnsiString;
55  parameter.Direction = ParameterDirection.Input;
56  parameter.Value = "Hello World!";
57 
58  command.Parameters.Add(parameter);
59 
60  // Execute the INSERT statement
61  command.ExecuteNonQuery();
62  command.Parameters.Clear();
63 
64  // Commit the transaction
65  rdmtrans.Commit();
66 
67  command.CommandText = queryString;
68 
69  // Execute a select statement which should return results
70  reader = command.ExecuteReader();
71 
72  // Read all of the results
73  while (reader.Read())
74  {
75  // Print out each row of the select in a newline
76  Console.WriteLine(reader.GetString(0));
77  }
78 
79  // Close the reader since retrieved all rows
80  reader.Close();
81 
82  // Close the connection
83  connection.Close();
84  }
85  catch (Exception ex)
86  {
87  Console.WriteLine(ex.Message);
88  }
89  }
90  }
91 }
92