- Back to Home »
- ASP.Net »
- DBManager
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;
using System.Collections.Specialized;
using System.Text;
using System.Data.Odbc;
using ESNAS;
using ESNLib;
public class DBManager
{
public static string connStr;
public static string USATIME = "05:00:00";
// total commands to execute
public static int intCommands = 0;
// the string array with queries
public static string[] QueryCollection = new string[1];
public DBManager()
{
//
// TODO: Add constructor logic here
//
// connectionString = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
}
public static string connectionString
{
get
{
return connStr;
}
set
{
connStr = value;
}
}
public static OdbcConnection getConnection(string connString)
{
try
{
OdbcConnection conn = new OdbcConnection(connString);
conn.Open();
return conn;
}
catch (Exception ex)
{
Log objLog = new Log("getConnection", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, ex.Message);
LogAs.LogException(objLog);
}
//INSTANT C# NOTE: Inserted the following 'return' since all code paths must return a value in C#:
return null;
}
public static DataTable GetDataTableFromQuery(string strQuery)
{
OdbcConnection myconn = getConnection();
OdbcCommand mycmd = new OdbcCommand();
mycmd.Connection = myconn;
mycmd.CommandType = CommandType.Text;
mycmd.CommandText = strQuery;
OdbcDataAdapter result = new OdbcDataAdapter(mycmd);
//query_found_rows = "select sql_calc_found_rows from
DataSet mydataset = new DataSet();
result.Fill(mydataset);
closeConnection(myconn);
return mydataset.Tables[0]; // make a table
}
public static DataSet GetDataSet(string strQuery)
{
OdbcConnection myconn = getConnection();
OdbcCommand mycmd = new OdbcCommand();
mycmd.Connection = myconn;
mycmd.CommandType = CommandType.Text;
mycmd.CommandText = strQuery;
OdbcDataAdapter result = new OdbcDataAdapter(mycmd);
//query_found_rows = "select sql_calc_found_rows from
DataSet mydataset = new DataSet();
result.Fill(mydataset, "DTCum");
closeConnection(myconn);
return mydataset;
}
public static string GetScalarstring(string query)
{
string str = "";
OdbcConnection myconn = new OdbcConnection();
OdbcCommand mycmd = new OdbcCommand();
try
{
myconn = getConnection();
mycmd.Connection = myconn;
mycmd.CommandType = CommandType.Text;
mycmd.CommandText = query;
str = mycmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
Log objLog = new Log("GetObjectFromDB", query, string.Empty, string.Empty, string.Empty, string.Empty, ex.Message);
LogAs.LogException(objLog);
return str;
}
finally
{
closeConnection(myconn);
}
return str;
}
public static string GetStringFromByte(object valuegot)
{
string returnstr = Convert.ToString(valuegot);
if (valuegot.GetType().ToString() == "System.Byte[]")
{
returnstr = Encoding.ASCII.GetString((System.Byte[])valuegot);
}
return returnstr;
}
public static ArrayList GetNameValueColl(string query)
{
try
{
OdbcConnection myconn = getConnection();
OdbcCommand mycmd = new OdbcCommand();
mycmd.Connection = myconn;
mycmd.CommandType = CommandType.Text;
mycmd.CommandText = query;
OdbcDataAdapter result = new OdbcDataAdapter(query, myconn);
DataSet mydataset = new DataSet();
result.Fill(mydataset);
closeConnection(myconn);
if (mydataset.Tables.Count > 0)
{
DataTable myTable = mydataset.Tables[0];
//INSTANT C# NOTE: Commented this declaration since looping variables in 'foreach' loops are declared in the 'foreach' header in C#
// DataRow myRow = null;
//INSTANT C# NOTE: Commented this declaration since looping variables in 'foreach' loops are declared in the 'foreach' header in C#
// DataColumn myColumn = null;
string tempkey = null;
string tempval = null;
int lb = 0;
ArrayList myarray = new ArrayList();
foreach (DataRow myRow in myTable.Rows)
{
NameValueCollection tempnvc = new NameValueCollection();
foreach (DataColumn myColumn in myTable.Columns)
{
if (!(myRow[myColumn] == DBNull.Value))
{
tempkey = myColumn.ColumnName;
tempval = GetStringFromByte(myRow[myColumn]);
tempnvc.Add(tempkey, tempval);
}
else
{
tempnvc.Add(myColumn.ColumnName, "");
}
}
myarray.Add(tempnvc);
lb = lb + 1;
}
return myarray;
}
else
{
return null;
}
}
catch (Exception ex)
{
Log objLog = new Log("GetNameValueColl", query, string.Empty, string.Empty, string.Empty, string.Empty, ex.Message);
LogAs.LogException(objLog);
return null;
}
}
public static OdbcConnection getConnection()
{
try
{
//string connstr ="DSN=TRIVIADSN;";
//string connstr = "Driver={MySQL ODBC 3.51 Driver};Server=90.0.0.14;Port=3306;Database=testtrivia;User=root; Password=root;";
string connstr = ConfigurationManager.AppSettings.Get("ConnectionString").ToString();
OdbcConnection conn = new OdbcConnection(connstr);
conn.Open();
return conn;
}
catch (Exception ex)
{
Log objLog = new Log("getConnection", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, ex.Message);
LogAs.LogException(objLog);
}
//INSTANT C# NOTE: Inserted the following 'return' since all code paths must return a value in C#:
return null;
}
public static void closeConnection(OdbcConnection conn)
{
try
{
conn.Close();
}
catch (Exception ex)
{
Log objLog = new Log("closeConnection", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, ex.Message);
LogAs.LogException(objLog);
}
}
public static long RunInsertQuery(ESNLib.BaseClass obj)
{
string insertQuery = obj.GetInsertQuery();
return RunQuery(insertQuery, true);
}
public static long RunQuery(string query, bool getLastId)
{
OdbcConnection MyConn = null;
OdbcTransaction tran = null;
long lastid = 0;
try
{
MyConn = DBManager.getConnection();
tran = MyConn.BeginTransaction();
OdbcCommand MyCmd = new OdbcCommand(query, MyConn, tran);
MyCmd.ExecuteNonQuery();
if (getLastId)
{
MyCmd.CommandText = "select LAST_INSERT_ID()";
lastid = Convert.ToInt64(MyCmd.ExecuteScalar());
}
tran.Commit();
return lastid;
}
catch (Exception ex)
{
if (tran != null)
tran.Rollback();
return -1;
}
finally
{
if (MyConn != null)
DBManager.closeConnection(MyConn);
}
}
}