Products Support Documentation Download
tutorials/HelloWorldADO/hello_world_ADOTutorial_main.cs
/*********************************************************************
** 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=localhost;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);
}
}
}
}