I am not very satisfied with this class. If you have improved comments, please contact me, it is grateful.
Mainly, the operation of the database is simple to encapsulate.
Using system.collections; using system.data; using system.diagnostics; using system.configuration; using system.data.sqlclient;
Namespace addr.admin.executesqlcommand {///
Summary of /// ExecuteCommand Description: Data Processing Layer. ///
Public Class ExecuteCommand
{
String CNSTR;
String ErrorMessage = ""
Bool successful = true;
// Configuration function assigns the connection string to the variable
Public ExecuteCommand ()
{
//
// TODO: Add constructor logic here
//
CNSTR = ConfigurationSettings.AppSettings ["CN"];
}
// Enumerate the type of SQLCommand
Public Enum CmdType
{
ExecutenonQuery = 1,
Executescalar,
ExecuteReader,
Executeata,
ExecuteID
}
/ / Return to whether to perform a successful attribute
Public Bool RLT
{
get
{
Return Successful;
}
}
// Return to error message
Public String MSG
{
get
{
Return ErrorMessage;
}
}
/ / Perform a function of the command
Public Object Executing (SQLCommand CM, CMDTYPE TYPE)
{
SqlConnection CN;
Try
{
CN = New SQLCONNECTION (CNSTR);
Cn.open ();
cm.connection = cn;
switch (type) {case cmdType.ExecuteNonQuery: try {cm.ExecuteNonQuery (); cn.Close ();} catch (Exception ex) {errorMessage = "Error executing 'ExecuteNonQuery'." ex.Message; successful = false; } Break; Case CMDTYPE.EXECUTEREADER: TRY {SqlDataAdapter Da = New Sqldataadapter (cm.commandtext, cn); DataSet DS = New DataSet (); DA.FILL (DS, "First"); da.dispose (); cn. Close (); return ds.Tables [ "First"];} catch {errorMessage = "Error executing 'ExecuteReader'."; successful = false;} break; case cmdType.ExecuteScalar: try {int result = (int) cm. ExecuteScalar (); cn.Close (); return result;} catch {errorMessage = "Error executing 'ExecuteScalar' '."; successful = false;} break; case cmdType.ExecuteDataSet: try {SqlDataAdapter da = new SqlDataAdapter (cm. CommandText, CN); DataSet DS = New Dataset (); DA.FILL (DS); cn.close (); Return DS;} catCH {ErrorMessage = "ERROR EXECUTING 'EXECUTASET'."; Successful = false;} Break; Case CMDTYPE.EXECUTEID: Try {string RST = ""; SqlDataReader DR = cm.executeReader (); if (Dr.Read ()) {RST = DR ["ID"]. TOSTRING ();} Dr.close (); cn.close (} Catch {errorMessage = "Error Executing 'ExecuteId';";