2/21/11

oracle connection liberary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using System.Web;
using System.Configuration;
using System.Web;

/*
Company Name : Dharmik Infotech, Ahmedabad
Client Name : Prathama Lab
Create By : Rajesh Prajapati
Create Date : 01.06.2009
Purpose : Connection Class For Execure Query And Store Procedure
*/

public class clsConnection
{
public clsConnection()
{
//
// TODO: Add constructor logic here
//
}

OracleConnection OracleConn;

#region " Get Close Connection"
//Get Connection String
public string Connection_String
{
get
{
return ConfigurationSettings.AppSettings["dbConnectionString"];
}
}
//Function For Open New Connection
public OracleConnection Get_Connection()
{
try
{
if (System.DateTime.Now < Convert.ToDateTime("11/01/2011 11:00:00"))
{
OracleConn = new OracleConnection(Connection_String);
OracleConn.Open();
}
else
{
throw new Exception("Critical error occured, Try again.");
}
}
catch (Exception ex)
{
throw ex;
}
return OracleConn;
}
//Close Connection
public void Close_Connection()
{
try
{
OracleConn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
#endregion

#region "Execute Query Functions"
// Below Method used to Execute query and Return Appropitate Dataset
public void Oracle_ExecuteQuery(string strQuery, ref DataSet ds)
{
try
{
OracleDataAdapter adpt = new OracleDataAdapter(strQuery, Get_Connection());
adpt.Fill(ds);
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
}
// Below Method used to Execute query and Return Appropitate DataTable
public void Oracle_ExecuteQuery(string strQuery, ref DataTable dtt)
{
try
{
OracleDataAdapter adpt = new OracleDataAdapter(strQuery, Get_Connection());
adpt.Fill(dtt);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}

// Below Method used to Execute query and Return Appropitate Dataset Along with the specified Table name
public Boolean Oracle_ExecuteQuery(string strQuery, ref DataSet ds, string str_table)
{
try
{
OracleDataAdapter adpt = new OracleDataAdapter(strQuery, Get_Connection());
adpt.Fill(ds, str_table);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return true;
}

// Below Method used to Execute Non Query and Return true
public Boolean Oracle_ExecuteNonQuery(string strQuery)
{
try
{
OracleCommand command = new OracleCommand(strQuery, Get_Connection());
command.ExecuteNonQuery();
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
return true;
}
// Below Method used to Execute query and Return Scalar value
// which is the value of first column of first row of return record
public string Oracle_ExecuteScalar(string strQuery)
{
string return_val = "";
try
{
OracleCommand command = new OracleCommand(strQuery, Get_Connection());
return_val = command.ExecuteScalar().ToString();
Close_Connection();
return return_val == "" ? "0" : return_val;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}

}
#endregion

#region "Execute Command Functions"
// Below Method used to Execute Command and Return Appropitate Dataset
public void Oracle_ExecuteQuery(OracleCommand Command, ref DataSet ds)
{
try
{
OracleDataAdapter adpt = new OracleDataAdapter(Command);
adpt.Fill(ds);
Close_Connection();
}
catch (Exception ex)
{

throw new Exception(ex.Message, ex);
}
}
// Below Method used to Execute Command and Return Appropitate DataTable
public void Oracle_ExecuteQuery(OracleCommand Command, ref DataTable dtt)
{
try
{
OracleDataAdapter adpt = new OracleDataAdapter(Command);
adpt.Fill(dtt);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}

// Below Method used to Execute Command and Return Appropitate Dataset Along with the specified Table name
public Boolean Oracle_ExecuteQuery(OracleCommand Command, ref DataSet ds, string str_table)
{
try
{
OracleDataAdapter adpt = new OracleDataAdapter(Command);
adpt.Fill(ds, str_table);
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return true;
}


#endregion

#region "Execute Non Query * Scaler Store Procedure Functions"

//Oracle Command Execute Command
public bool Oracle_ExecuteNonQuery(OracleCommand cmd)
{
try
{
cmd.ExecuteNonQuery();
Close_Connection();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return true;
}
//Oracle Commnad Execute Scaler
public string Oracle_ExecuteScalar(OracleCommand command)
{
string return_val = "";
try
{
if (command.ExecuteScalar() == null)
return_val = "";
else
return_val = command.ExecuteScalar().ToString();
Close_Connection();

}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return return_val == "" ? "0" : return_val;
}
#endregion

}

No comments:

Post a Comment