Tuesday, December 15, 2015

Ingres data access layer in C#

We had discussed about Ingres as our database for application development. So lets move ahead and implement an Ingres supported data access layer.

Simply copy the following class and add it to your data layer project as your data handler.

 using System;  
 using System.Collections.Generic;  
 using System.Text;  
 using System.Data;  
 using Ingres;  
 using Ingres.Client;  
 using System.Collections;  
 public class DataHandle  
 {  
   #region - Class Properties -  
     public IngresConnection Connection;  
   #endregion  
   #region - Constructors -  
     public DataHandle(string connectionString)  
     {  
       Connection = new IngresConnection(connectionString);  
     }  
     public DataHandle()  
     {  
       Connection = new IngresConnection(ConnectionString);  
     }  
   #endregion  
 public IngresDataReader ExecuteQuery(string sql)  
     {  
       IngresCommand ingresCommand = null;  
       try  
       {  
         if (this.Connection.State != ConnectionState.Open)  
           this.Connection.Open();  
         ingresCommand = new IngresCommand(sql, this.Connection);  
         ingresCommand.CommandType = CommandType.Text;  
         ingresCommand.CommandTimeout = 0;  
         IngresDataReader ingresDataReader = ingresCommand.ExecuteReader(CommandBehavior.CloseConnection);  
         return ingresDataReader;  
       }  
       catch  
       {  
         throw;  
       }  
       finally  
       {  
         if (ingresCommand != null)  
           ingresCommand.Dispose();  
       }  
}  
public IngresDataReader ExecuteQuery(string sql, IngresParameterCollection parameters)  
     {  
       IngresCommand ingresCommand = null;  
       try  
       {  
         if (this.Connection.State != ConnectionState.Open)  
           this.Connection.Open();  
         ingresCommand = new IngresCommand(sql, this.Connection);  
         ingresCommand.CommandType = CommandType.Text;  
         ingresCommand.CommandTimeout = 0;  
         if (parameters != null)  
         {  
           foreach (IngresParameter parameter in parameters)  
           {  
             ingresCommand.Parameters.Add(parameter);  
           }  
         }  
         IngresDataReader ingresDataReader = ingresCommand.ExecuteReader(CommandBehavior.CloseConnection);  
         return ingresDataReader;  
       }  
       catch  
       {  
         throw;  
       }  
       finally  
       {  
         if (ingresCommand != null)  
           ingresCommand.Dispose();  
       }  
}  
 public int ExecuteNonQuery(string sql)  
     {  
       try  
       {  
         if (this.Connection.State != ConnectionState.Open)  
           this.Connection.Open();  
         IngresCommand ingresCommand = new IngresCommand(sql, this.Connection);  
         ingresCommand.CommandTimeout = 0;  
         int numberOfRecordsAffected = ingresCommand.ExecuteNonQuery();  
         return numberOfRecordsAffected;  
       }  
       catch  
       {  
         throw;  
       }  
       finally  
       {  
         if (this.Connection != null)  
           Connection.Close();  
       }  
}  
public int ExecuteNonQuery(string sql, IngresParameterCollection parameters)  
     {  
       try  
       {  
         if (this.Connection.State != ConnectionState.Open)  
           this.Connection.Open();  
         IngresCommand ingresCommand = new IngresCommand(sql, this.Connection);  
         ingresCommand.CommandTimeout = 0;  
         foreach (IngresParameter parameter in parameters)  
         {  
           ingresCommand.Parameters.Add(parameter);  
         }  
         int numberOfRecordsAffected = ingresCommand.ExecuteNonQuery();  
         return numberOfRecordsAffected;  
       }  
       catch  
       {  
         throw;  
       }  
       finally  
       {  
         if (this.Connection != null)  
           Connection.Close();  
       }  
     }  
 }  

Simply as in SQL Server the ExecuteNonQuery will return an integer as an return type which you can track any error inside the method.

ExecuteQuery will return a IngresDataReader object that you can loop through till it ends and append the data into a list or a collection.

So this simple class will make you easy for using ingres and the only disadvantage here is to use sql queries as inputs to the methods.

It is also possible to use Transactions which in Ingres it is called, IngresTransaction class. You may try adding an IngresTransaction to this class and make it more valuable to the project.

Comment if you find it is interesting or need some help on this class.




No comments:

Post a Comment