using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Windows.Forms; using System.Collections; namespace Database {///
Public Database () {// // Todo: Add Constructor Logic to here
Xmlread constr = new xmlread (); if (constr.readallconnectNode ()) {constr = constr.connstring; // try // {// open (); //} // catch (exception ex) // {// Messagebox.show ("Database Connection Error" EX.TOSTRING ()); //}
} Else {constr = "- 1"; // throw new sqlerrorcollection;
}
// public bool open () // {// mcn.connectionstring = constr; // try // {// mcn.open (); //} // Catch (Exception) // {// Return FALSE; / /} // return true; ///}
///
// public virtual int getdata (String TmpMomputername) // {// Return-1; //}
#Region ExecutenonQuery
///
///
Public int executenonquery (string sqltext) {return executenonQuery (commandtype.text, sqltext);}
///
public bool ExecuteNonQuery (string connString, CommandType cmdType, CommandEnum.DsCommandType dsCommandType, string cmdText, DataSet dataset, string tablename, params SqlParameter [] cmdParms) {SqlDataAdapter dsCommand = new SqlDataAdapter (); SqlCommand cmd = new SqlCommand (); using (SqlConnection Conn = new sqlconnection (conn.State! = connectionState.Open) Conn (); cmd.connection = conn; cmd.commandtext = cmdText; cmd.commandtype = cmdtype;
IF (cmdparms! = null) {Foreach (Sqlparameter Parm in cmdparms) cmd.parameters.add (parm);}
switch (dsCommandType) {case CommandEnum.DsCommandType.InsertCommand: dsCommand.InsertCommand = cmd; break; case CommandEnum.DsCommandType.UpdateCommand: dsCommand.UpdateCommand = cmd; break; case CommandEnum.DsCommandType.DeleteCommand: dsCommand.DeleteCommand = cmd; break; DEFAULT: BREAK;
DScommand.Update; if (dataset.haserror) {dataset.tables [TableName] .GeterrorS () [0] .clearerrrs (); Return False;} else {dataset.acceptchanges (); return;} }
///
if (cmdInsertText = String.Empty!) {SqlCommand cmdInsert = new SqlCommand (); cmdInsert.Connection = conn; cmdInsert.CommandText = cmdInsertText; cmdInsert.CommandType = cmdInsertType; if (! cmdInsertParms = null) {foreach (SqlParameter parm in cmdInsertParms ) cmdInsert.Parameters.Add (parm);} dsCommand.InsertCommand = cmdInsert;!} if (cmdUpdateText = String.Empty) {SqlCommand cmdUpdate = new SqlCommand (); cmdUpdate.Connection = conn; cmdUpdate.CommandText = cmdUpdateText; cmdUpdate. CommandType = cmdupdatetype; if (cmdupdateparms! = Null) {Foreach (SQLParameter Parm in cmdupdateparms) cmdupdate.parameters.add (PARM);} dscommand.u PDATECOMMAND = cmdupdate;
if (cmdDeleteText = String.Empty!) {SqlCommand cmdDelete = new SqlCommand (); cmdDelete.Connection = conn; cmdDelete.CommandText = cmdDeleteText; cmdDelete.CommandType = cmdDeleteType; if (! cmdDeleteParms = null) {foreach (SqlParameter parm in cmdDeleteParms ) Cmddelete.parameters.add (parm);} dscommand.deleteCommand = cmddelete;}
if (cmdInsertText == String.Empty && cmdUpdateText == String.Empty && cmdDeleteText == String.Empty) {SqlCommandBuilder scb = new SqlCommandBuilder (dsCommand); return false;} dsCommand.Update (dataset, tablename);
IF (dataset.tables "{dataset.tables [Tablename] .geterrors () [0] .clearerrrs (); return false;} else {dataset.acceptchanges (); return true;}}}
#ndregion #Region ExecuteReader
///
public SqlDataReader ExecuteReader (string connString, CommandType cmdType, string cmdText, params SqlParameter [] cmdParms) {SqlCommand cmd = new SqlCommand (); SqlConnection conn = new SqlConnection (connString);
try {PrepareCommand (cmd, conn, null, cmdType, cmdText, cmdParms); SqlDataReader rdr = cmd.ExecuteReader (CommandBehavior.CloseConnection); cmd.Parameters.Clear (); return rdr;} catch {conn.Close (); throw }}
///
try {PrepareCommand (cmd, conn, null, cmdType, cmdText, cmdParms); SqlDataReader rdr = cmd.ExecuteReader (CommandBehavior.CloseConnection); cmd.Parameters.Clear (); return rdr;} catch {conn.Close (); throw }}
///
Public SqlDataReader ExecuteReader (String cmdtxt) {sqlcommand cmd = new sqlcommand (); sqlConnection conn = new sqlConnection (constr);
Try {cmd = new sqlcommand (cmdtxt, conn); conn.open (); SqlDataReader Rdr = cmd.executeReader (Commandbehavior.CloseConnection); Return Rdr;} catch {conn.close (); throw;}}
#ndregion #Region Private function
///
private void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter [] cmdParms) {if (conn.State = ConnectionState.Open!) {try {conn.Open ();} catch (Exception Ex ) {Throw ex; // string a = ex.Tostring (); // Return;}} cmd.connection = conn; cmd.commandtext = cmdText; if (trans! = Null) cmd.transaction = trans;
cmd.commandtype = cmdtype;
IF (cmdparms! = null) {Foreach (Sqlparameter Parm in cmdparms) cmd.parameters.add (parm);}}
private void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameterCollection cmdParms) {if (conn.State = ConnectionState.Open!) conn.Open ();
CMD.Connection = conn; cmd.commandtext = cmdtext;
IF (trans! = null) cmd.Transaction = Trans;
cmd.commandtype = cmdtype;
IF (cmdparms! = null) {Foreach (Sqlparameter Parm in cmdparms) cmd.parameters.add (parm);}}
///
Private sqlparameter newfieldparam (string fld) {sqlparameter param = new sqlparameter (); param.ParameterName = "@" fld; param.sourceColumn = fld; return param;}
///
///
public void FillData (CommandType cmdType, string cmdText, DataSet dataset, string tablename, params SqlParameter [] cmdParms) {SqlDataAdapter dsCommand = new SqlDataAdapter (); SqlCommand cmd = new SqlCommand (); dsCommand.SelectCommand = cmd; //dsCommand.TableMappings .Add ("Table"; USING (SqlConnection Conn = New SqlConnection (constr)) {PrepareCommand (CMD, CMDText, cmdparms); dscommand.fill (Dataset, TableName);}}
///
internal string constr = null; // = "Uid = sa; Pwd = sa; Server = Drago; Database = Northwind"; internal SqlConnection mcn = new SqlConnection (); internal DataSet m_DataSet = new System.Data.DataSet ();} }