Publish date: 2023-06-16

Resetting the session settings before reusing a connection (C#, FB4)

This example demonstrates the implicit execution of «ALTER SESSION RESET» when reusing a connection.

The example requires Firebird v4 and IBProvider v5.30.

////////////////////////////////////////////////////////////////////////////////
//Samples for LCPI ADO.NET Data provider for OLEDB.
//                                                    ibprovider.com. 16.06.2023
using System;

namespace Sample_0032{
////////////////////////////////////////////////////////////////////////////////
using xdb=lcpi.data.oledb;
////////////////////////////////////////////////////////////////////////////////
//class Program

class Program
{
 private const string c_cn_str
  ="provider=LCPI.IBProvider.5;"
  +"location=inet4://localhost/d:\\database\\fb_04_0_0\\employee.fdb;"
  +"dbclient_type=fb.direct;"
  +"user id=SYSDBA;"
  +"password=masterkey;";

 private const string c_test_sql
  ="select cast('1234567890123456.5' as DECFLOAT(16)) from RDB$DATABASE";

 //-----------------------------------------------------------------------
 static int Main(string[] args)
 {
  int result = 0;

  try
  {
   Console.WriteLine("TEST SQL: {0}",c_test_sql);
   Console.WriteLine("");

   for(int pass=0;pass!=3;)
   {
    ++pass;

    Console.WriteLine("-------------------- PASS {0}",pass);

    Exec();
   }
  }
  catch(Exception e)
  {
   ++result;
   Console.WriteLine("ERROR: {0}",e.Message);
  }//catch

  return result;
 }//Main

 //-----------------------------------------------------------------------
 private static void Exec()
 {
  using(var cn=new xdb.OleDbConnection(c_cn_str))
  {
   Console.WriteLine("Open connection ...");

   cn.Open();

   using(var tr=cn.BeginTransaction())
   {
    using(var cmd=new xdb.OleDbCommand("select CURRENT_CONNECTION from RDB$DATABASE",cn,tr))
     Console.WriteLine("Connection ID  : {0}",cmd.ExecuteScalar());

    // expected result: 1234567890123457  [HALF_UP round]
    using(var cmd=new xdb.OleDbCommand(c_test_sql,cn,tr))
     Console.WriteLine("Default Round  : {0}",cmd.ExecuteScalar());

    using(var cmd=new xdb.OleDbCommand("SET DECFLOAT ROUND HALF_EVEN",cn,tr))
     cmd.ExecuteNonQuery();

    // expected result: 1234567890123456 [HALF_EVEN round]
    using(var cmd=new xdb.OleDbCommand(c_test_sql,cn,tr))
     Console.WriteLine("HALF_EVEN Round: {0}",cmd.ExecuteScalar());

    tr.Commit();
   }//using tr
  }//using cn
 }//Exec
}//class Program

////////////////////////////////////////////////////////////////////////////////
}//namespace Sample_0032


Output of sample.
Output.