using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using TrackComm.BI;
namespace TrackComm
{
public class SQL
{
#region Variables and Object Declaration
private OleDbTransaction tranNew;
//private SqlTransaction tranNew;
OleDbConnection con = new OleDbConnection();
//private SqlConnection con = new SqlConnection();
private string strSqlConn;
//public SqlParameter objTempParm;
#endregion
//OleDbConnection conn = newOleDb.OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=C:\\myData.xls; " + "Extended Properties=Excel 8.0;")));
#region Methods related to connection and disconnection of database
public bool Connect()
{
try
{
//strSqlConn = "Provider="+Functions.PROVIDER+"; Data Source=" + Functions.DATASOURCE + "";
//strSqlConn = "provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=C:\\myData.xls; " + "Extended Properties=Excel 8.0";
if (VariableInfo.mVersion == "12.0")
{
strSqlConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + VariableInfo.mDbPath;
}
else if (VariableInfo.mVersion == "4.0")
{
strSqlConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + VariableInfo.mDbPath;
}
strSqlConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + VariableInfo.mDbPath;
// strSqlConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Application.StartupPath + "\\Bond_Printing_App.mdb";
if (con.State == ConnectionState.Closed)
{
con.ConnectionString = strSqlConn;
con.Open();
return true;
}
else if (con.State == ConnectionState.Open)
{
con.Close();
con.ConnectionString = strSqlConn;
con.Open();
return true;
}
return false;
}
catch
{
throw;
}
}
public void Disconnect()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
catch
{
throw;
}
}
#endregion
#region Transaction related methods
public void startTransaction()
{
try
{
if (Connect() == true)
tranNew = con.BeginTransaction();
// tranNew = con.BeginTransaction();
}
catch
{
throw;
}
}
public void CommitTransaction()
{
try
{
tranNew.Commit();
tranNew.Dispose();
Disconnect();
}
catch
{
throw;
}
}
public void RollbackTransaction()
{
try
{
tranNew.Rollback();
tranNew.Dispose();
Disconnect();
}
catch
{
throw;
}
}
#endregion
#region ExecuteScaler methods
public int ExecuteScalar(string qry)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand(qry, con);
//SqlCommand cmd = new SqlCommand(qry, con);
cmd.CommandText = qry;
int i = (int)cmd.ExecuteScalar();
cmd.Dispose();
Disconnect();
return i;
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
public int ExecuteScalarTran(string qry)
{
try
{
OleDbCommand cmd = new OleDbCommand(qry, con, tranNew);
//SqlCommand cmd = new SqlCommand(qry, con, tranNew);
cmd.CommandText = qry;
int i = (int)cmd.ExecuteScalar();
cmd.Dispose();
return i;
}
catch
{
throw;
}
}
#endregion
#region ExecuteNonQuery methods
public void ExecuteNonQuery(string qry)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand(qry, con);
// SqlCommand cmd = new SqlCommand(qry, con);
cmd.CommandText = qry;
cmd.ExecuteNonQuery();
cmd.Dispose();
Disconnect();
}
else
{
throw new Exception("database connection not found");
}
}
catch(Exception ex)
{
throw;
}
}
public void ExecuteNonQueryTran(string qry)
{
try
{
OleDbCommand cmd = new OleDbCommand(qry, con, tranNew);
//SqlCommand cmd = new SqlCommand(qry, con, tranNew);
cmd.CommandText = qry;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch
{
throw;
}
}
#endregion
#region ExecuteDataset methods
public DataSet ExecuteDataset(string qry)
{
try
{
if (Connect() == true)
{
OleDbDataAdapter Oracleda = new OleDbDataAdapter(qry, con);
//SqlDataAdapter Oracleda = new SqlDataAdapter(qry, con);
DataSet ds = new DataSet();
Oracleda.Fill(ds);
Oracleda.Dispose();
ds.Dispose();
return ds;
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
public DataSet ExecuteDatasetTran(string qry)
{
try
{
OleDbCommand cmd = new OleDbCommand(qry, con, tranNew);
//SqlCommand cmd = new SqlCommand(qry, con, tranNew);
cmd.CommandText = qry;
OleDbDataAdapter Oracleda = new OleDbDataAdapter(cmd);
// SqlDataAdapter Oracleda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
Oracleda.Fill(ds);
Oracleda.Dispose();
ds.Dispose();
cmd.Dispose();
return ds;
}
catch
{
throw;
}
}
#endregion
#region ExecuteProcedure methods
public string ExecuteProcedure(string Proc, SqlParameter[] param, string err)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand();
//SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(param);
cmd.CommandText = Proc;
cmd.ExecuteNonQuery();
Disconnect();
return cmd.Parameters[err].Value.ToString();
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
public string ExecuteProcedureTran(string Proc, SqlParameter[] param, string err)
{
try
{
OleDbCommand cmd = new OleDbCommand();
//SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = tranNew;
cmd.Parameters.AddRange(param);
cmd.CommandText = Proc;
cmd.ExecuteNonQuery();
Disconnect();
return cmd.Parameters[err].Value.ToString();
}
catch
{
throw;
}
}
public string ExecuteProcedureParam(string Proc, SqlParameter[] param, string err, string result)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand();
//SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(param);
cmd.CommandText = Proc;
cmd.ExecuteNonQuery();
Disconnect();
if (cmd.Parameters[result].Value.ToString() != "")
{
return cmd.Parameters[result].Value.ToString();
}
else
{
return cmd.Parameters[err].Value.ToString();
}
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using TrackComm.BI;
namespace TrackComm
{
public class SQL
{
#region Variables and Object Declaration
private OleDbTransaction tranNew;
//private SqlTransaction tranNew;
OleDbConnection con = new OleDbConnection();
//private SqlConnection con = new SqlConnection();
private string strSqlConn;
//public SqlParameter objTempParm;
#endregion
//OleDbConnection conn = newOleDb.OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=C:\\myData.xls; " + "Extended Properties=Excel 8.0;")));
#region Methods related to connection and disconnection of database
public bool Connect()
{
try
{
//strSqlConn = "Provider="+Functions.PROVIDER+"; Data Source=" + Functions.DATASOURCE + "";
//strSqlConn = "provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=C:\\myData.xls; " + "Extended Properties=Excel 8.0";
if (VariableInfo.mVersion == "12.0")
{
strSqlConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + VariableInfo.mDbPath;
}
else if (VariableInfo.mVersion == "4.0")
{
strSqlConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + VariableInfo.mDbPath;
}
strSqlConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + VariableInfo.mDbPath;
// strSqlConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Application.StartupPath + "\\Bond_Printing_App.mdb";
if (con.State == ConnectionState.Closed)
{
con.ConnectionString = strSqlConn;
con.Open();
return true;
}
else if (con.State == ConnectionState.Open)
{
con.Close();
con.ConnectionString = strSqlConn;
con.Open();
return true;
}
return false;
}
catch
{
throw;
}
}
public void Disconnect()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
catch
{
throw;
}
}
#endregion
#region Transaction related methods
public void startTransaction()
{
try
{
if (Connect() == true)
tranNew = con.BeginTransaction();
// tranNew = con.BeginTransaction();
}
catch
{
throw;
}
}
public void CommitTransaction()
{
try
{
tranNew.Commit();
tranNew.Dispose();
Disconnect();
}
catch
{
throw;
}
}
public void RollbackTransaction()
{
try
{
tranNew.Rollback();
tranNew.Dispose();
Disconnect();
}
catch
{
throw;
}
}
#endregion
#region ExecuteScaler methods
public int ExecuteScalar(string qry)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand(qry, con);
//SqlCommand cmd = new SqlCommand(qry, con);
cmd.CommandText = qry;
int i = (int)cmd.ExecuteScalar();
cmd.Dispose();
Disconnect();
return i;
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
public int ExecuteScalarTran(string qry)
{
try
{
OleDbCommand cmd = new OleDbCommand(qry, con, tranNew);
//SqlCommand cmd = new SqlCommand(qry, con, tranNew);
cmd.CommandText = qry;
int i = (int)cmd.ExecuteScalar();
cmd.Dispose();
return i;
}
catch
{
throw;
}
}
#endregion
#region ExecuteNonQuery methods
public void ExecuteNonQuery(string qry)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand(qry, con);
// SqlCommand cmd = new SqlCommand(qry, con);
cmd.CommandText = qry;
cmd.ExecuteNonQuery();
cmd.Dispose();
Disconnect();
}
else
{
throw new Exception("database connection not found");
}
}
catch(Exception ex)
{
throw;
}
}
public void ExecuteNonQueryTran(string qry)
{
try
{
OleDbCommand cmd = new OleDbCommand(qry, con, tranNew);
//SqlCommand cmd = new SqlCommand(qry, con, tranNew);
cmd.CommandText = qry;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch
{
throw;
}
}
#endregion
#region ExecuteDataset methods
public DataSet ExecuteDataset(string qry)
{
try
{
if (Connect() == true)
{
OleDbDataAdapter Oracleda = new OleDbDataAdapter(qry, con);
//SqlDataAdapter Oracleda = new SqlDataAdapter(qry, con);
DataSet ds = new DataSet();
Oracleda.Fill(ds);
Oracleda.Dispose();
ds.Dispose();
return ds;
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
public DataSet ExecuteDatasetTran(string qry)
{
try
{
OleDbCommand cmd = new OleDbCommand(qry, con, tranNew);
//SqlCommand cmd = new SqlCommand(qry, con, tranNew);
cmd.CommandText = qry;
OleDbDataAdapter Oracleda = new OleDbDataAdapter(cmd);
// SqlDataAdapter Oracleda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
Oracleda.Fill(ds);
Oracleda.Dispose();
ds.Dispose();
cmd.Dispose();
return ds;
}
catch
{
throw;
}
}
#endregion
#region ExecuteProcedure methods
public string ExecuteProcedure(string Proc, SqlParameter[] param, string err)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand();
//SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(param);
cmd.CommandText = Proc;
cmd.ExecuteNonQuery();
Disconnect();
return cmd.Parameters[err].Value.ToString();
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
public string ExecuteProcedureTran(string Proc, SqlParameter[] param, string err)
{
try
{
OleDbCommand cmd = new OleDbCommand();
//SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = tranNew;
cmd.Parameters.AddRange(param);
cmd.CommandText = Proc;
cmd.ExecuteNonQuery();
Disconnect();
return cmd.Parameters[err].Value.ToString();
}
catch
{
throw;
}
}
public string ExecuteProcedureParam(string Proc, SqlParameter[] param, string err, string result)
{
try
{
if (Connect() == true)
{
OleDbCommand cmd = new OleDbCommand();
//SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(param);
cmd.CommandText = Proc;
cmd.ExecuteNonQuery();
Disconnect();
if (cmd.Parameters[result].Value.ToString() != "")
{
return cmd.Parameters[result].Value.ToString();
}
else
{
return cmd.Parameters[err].Value.ToString();
}
}
else
{
throw new Exception("database connection not found");
}
}
catch
{
throw;
}
}
#endregion
}
}
No comments:
Post a Comment