Using system;
Using system.configuration;
Using system.data;
Using system.data.sqlclient;
Using system.collections;
Namespace mycorporation.Department.Database
{
///
/// General Database Class
/// summary>
Public Class Database
{
Private string connStr = NULL;
Public Database ()
{
Connstr = configurationSettings.appsettings ["connStr"];
}
Public Database (String STR)
{
Try
{
THIS.CONNSTR = STR;
}
Catch (Exception EX)
{
Throw EX;
}
}
///
/// Return Connection object
/// summary>
///
Public SqlConnection ReturnConn ()
{
SqlConnection conn = new SqlConnection (connStr);
Cn.open ();
Return conn;
}
Public Void Dispose (SqlConnection CONN)
{
IF (CONN! = NULL)
{
CONN.CLOSE ();
Conn.dispose ();
}
Gc.collect ();
}
///
/// run the SQL statement
/// summary>
/// param>
Public void Runproc (String SQL)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SQLCommand CMD;
CMD = CREATECMD (SQL, CONN);
Try
{
Cmd.executenonquery ();
}
Catch
{
Throw new Exception (SQL);
}
Dispose (CONN);
Return;
}
///
/// Run the SQL statement to return DataReader
/// summary>
/// param>
///
Public SqlDataReader RunprocgetReader (String SQL)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SQLCommand CMD;
CMD = CREATECMD (SQL, CONN);
SqlDataReader DR;
Try
{
DR = cmd.executeReader (commandbehavior.default);
}
Catch
{
Throw new Exception (SQL);
}
// Dispose (conn);
Return DR;
}
///
/// Generate a Command object /// summary>
/// param>
/// param>
///
Public SQLCommand CreateCMD (String SQL, SQLCONNECTION CONN)
{
SQLCommand CMD;
CMD = New SQLCOMMAND (SQL, CONN);
Return CMD;
}
///
/// Generate a Command object
/// summary>
/// param>
///
Public SqlCommand CreateCMD (String SQL)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SQLCommand CMD;
CMD = New SQLCOMMAND (SQL, CONN);
Return CMD;
}
///
/// Return Adapter object
/// summary>
/// param>
/// param>
///
Public SqlDataAdapter Createda (String SQL)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SqlDataAdapter Da;
Da = New SqlDataAdapter (SQL, CONN);
Return DA;
}
///
/// Run the SQL statement, return DataSet objects
/// summary>
/// SQL statement param>
/// DataSet object param>
Public DataSet Runproc (String SQL, Dataset DS)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SqlDataAdapter Da;
// da = Createda (SQL, CONN);
Da = New SqlDataAdapter (SQL, CONN);
Try
{
Da.fill (DS);
}
Catch (Exception Err)
{
Throw ERR;
}
Dispose (CONN);
Return DS;
}
///
/// Run the SQL statement, return DataSet objects
/// summary>
/// SQL statement param>
/// DataSet object param> /// table name param>
Public DataSet Runproc (String SQL, Dataset DS, String TableName)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SqlDataAdapter Da;
DA = Createda (SQL);
Try
{
Da.fill (DS, TABLENAME);
}
Catch (Exception EX)
{
Throw EX;
}
Dispose (CONN);
Return DS;
}
///
/// Run the SQL statement, return DataSet objects
/// summary>
/// SQL statement param>
/// DataSet object param>
/// Table name param>
Public DataSet Runproc (String SQL, DataSet DS, Int StartIndex, Int PageSize, String TableName)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SqlDataAdapter Da;
DA = Createda (SQL);
Try
{
Da.fill (DS, StartIndex, PageSize, TableName);
}
Catch (Exception EX)
{
Throw EX;
}
Dispose (CONN);
Return DS;
}
///
/// Test if data exists
/// summary>
///
Public Bool EXISTDATE (STRING SQL)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
SqlDataReader DR;
DR = CREATECMD (SQL, CONN) .executeReader ();
IF (Dr.Read ())
{
Dispose (CONN);
Return True;
}
Else
{
Dispose (CONN);
Return False;
}
}
///
/// Return to the first line of the first line of the SQL statement execution
/// summary>
///
Public String ReturnValue (String SQL)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
String result;
SqlDataReader DR;
Try
{
DR = CREATECMD (SQL, CONN) .executeReader ();
IF (Dr.Read ())
{
Result = DR [0] .toString (); Dr.close ();
}
Else
{
Result = "";
Dr.close ();
}
}
Catch
{
Throw new Exception (SQL);
}
Dispose (CONN);
Return Result;
}
///
/// Return the first column of the SQL statement, the columni column,
/// summary>
///
Public String ReturnValue (String SQL, INT Columni)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
String result;
SqlDataReader DR;
Try
{
DR = CREATECMD (SQL, CONN) .executeReader ();
}
Catch
{
Throw new Exception (SQL);
}
IF (Dr.Read ())
{
Result = DR [Columni] .tostring ();
}
Else
{
Result = "";
}
Dr.close ();
Dispose (CONN);
Return Result;
}
///
/// Generate a SQLCommand used in a stored procedure.
/// summary>
/// Stored procedure name. param>
/// Stored procedures into the parameter group. param>
///
Public SQLCommand CreateCMD (String Procname, Sqlparameter [] PRAMS)
{
SqlConnection conn;
CONN = New SqlConnection (Conntr);
Cn.open ();
Sqlcommand cmd = new sqlcommand (procname, conn);
Cmd.commandtype = commandtype.storedProcedure;
IF (PRAMS! = NULL)
{
Foreach (Sqlparameter Parameter In Prams)
{
IF (Parameter! = NULL)
{
Cmd.Parameters.Add (parameter);
}
}
}
Return CMD;
}
///
/// Generate a SQLCommand object for the stored procedure
/// summary>
/// Store process name param>
/// Stored procedure parameters param>
///
Private Sqlcommand CreateCMD (String Procname, Sqlparameter [] Prams, SqlDataReader DR)
{
SqlConnection conn;
CONN = New SqlConnection (conn.open ();
Sqlcommand cmd = new sqlcommand (procname, conn);
Cmd.commandtype = commandtype.storedProcedure;
IF (PRAMS! = NULL)
{
Foreach (Sqlparameter Parameter In Prams)
Cmd.Parameters.Add (parameter);
}
Cmd.parameters.add (
New Sqlparameter ("ReturnValue", SqldbType.Int, 4,
ParameterDirection.ReturnValue, False, 0, 0,
String.empty, DataRowVersion.default, null);
Return CMD;
}
///
/// Run the stored procedure, return.
/// summary>
/// Store process name param>
/// Stored procedure parameters param>
/// SqlDataReader object param>
Public void Runproc (String Procname, Sqlparameter [] Prams, SqlDataReader DR)
{
Sqlcommand cmd = createcmd (Procname, PRAMS, DR);
DR = cmd.executeReader (System.Data.commandbehavior.closeConnection);
Return;
}
///
/// Run the stored procedure, return.
/// summary>
/// Store process name param>
/// Stored procedure parameters param>
Public String RunProc (String Procname, Sqlparameter [] PRAMS)
{
SqlDataReader DR;
Sqlcommand cmd = createcmd (procname, prams);
DR = cmd.executeReader (System.Data.commandbehavior.closeConnection);
IF (Dr.Read ())
{
Return Dr.GetValue (0) .tostring ();
}
Else
{
""; "
}
}
///
/// Run the stored procedure and return to DataSet.
/// summary>
/// Stored procedure name. param>
/// Stored procedures into the parameter group. param>
///
Public DataSet Runproc (String Procname, Sqlparameter [] Prams, Dataset DS)
{
Sqlcommand cmd = createcmd (procname, prams);
SqlDataAdapter Da = New SqlDataAdapter (CMD);
Try
{
Da.fill (DS);
}
Catch (Exception EX)
{
Throw EX;
}
Return DS;
}
}
}