The database is discussed in front of the database, and now it is now true with C # encoding.
Here I will write the design and implementation, which should be clearer from the hierarchical structure. 4, the core code design core code is mainly encapsulated some common functions, and the subsequent data layer, the business layer code will inherit the core code, and uniformly use the functions provided by the core code. The core code class is an abstract class that cannot be instantiated. COOFUCOO.CORE.DBOBJECT: This class is all data layer class base classes Using system; use system.data; use system.data.sqlclient;
namespace Coofucoo.Core {/// /// DbObject is the class from which all classes in the Data Services /// Tier inherit. The core functionality of establishing a connection /// with the database and executing simple stored procedures IS also /// provided by this base class. /// summary> public abstract class dbobject {protected sqlConnection Connection;
/// /// a parameterized constructor, IT allows us to take a connection /// String as a constructor argument, automatically instantiarating /// a new connection. /// summary> /// Connection String to the associated database param> public dbObject (string newConnectionString) {connectionString = newConnectionString; Connection = new SqlConnection (connectionString);}
/// /// protected string /// to inheriting classes. Read-only. /// summary> protected string connectionstring {get {return connections;}}
/// /// private routine allowed Only by this base class, it automates the task /// of building a SQLCOMMAND OBJECT Designed to Obtain a return.com ///// summary > /// name of the stored procedure in the DB, eg. sp_DoTask param> /// Array of IDataParameter objects containing parameters to the stored proc < / param> /// Newly instantiated SqlCommand instance returns> private SqlCommand BuildIntCommand (string storedProcName, IDataParameter [] parameters) {SqlCommand command = BuildQueryCommand (storedProcName, parameters); command.Parameters.Add (new SqlParameter ( " RETURNVALUE, SQLDBTYPE.INT, 4, / * SIZE * / ParameterDirection.RtrunValue, false, / * is nullable * / 0, / * byte precision * / 0, / * byte scale * / string.empty, datarowversion.default, NULL);
Return Command;
/// /// builds a sqlcommand designed to return a sqldatarader, and not /// An actual integer value. /// summary> /// name of the store procedure param> /// Array of IDataParameter objects param> /// returns> private SqlCommand BuildQueryCommand (string storedProcName, IDataParameter [] parameters) {SqlCommand command = New SqlCommand (StoredProcname, Connection); Command.commandType = CommandType.StoredProcedure;
Foreach (Sqlparameter Parameter in Parameters) {Command.Parameters.Add (parameter);
Return Command;
}
/// /// Runs a stored procedure, can only be called by those classes deriving /// from this base. It returns an integer indicating the return value of the /// stored procedure, and also returns the value of the RowsAffected aspect /// of the stored procedure that is returned by the ExecuteNonQuery method. /// summary> /// name of the stored procedure param> /// < param name = "parameters"> Array of IDataParameter objects param> /// Number of rows affected by the stored procedure. param> /// An integer indicating return value of the stored procedure returns> protected int RunProcedure (string storedProcName, IDataParameter [] parameters, out int rowsAffected) {int result; Connection.Open (); SqlCommand command = BuildIntCommand (storedProcName, parameters); rowsAffected = command.ExecuteNonQuery ( ); Result = (int) Command.Parameters ["ReturnValue"]. Value; connection.close (); return result;}
/// /// Will Run A Stored Procedure, Can Only Be Called by Those Classes deriving //////////////procedure. /////pcedure. /// Summary> /// Name of the stored procedure param> /// array of parameters to be passed to the procedure param> /// < Returns> a newly instantiated SqldataReader Object returns> protected sqldatareader runprocedure (String StoredProcname) {sqlDataReader ReturnReader;
Connection.Open (); SqlCommand command = BuildQueryCommand (storedProcName, parameters); command.CommandType = CommandType.StoredProcedure; returnReader = command.ExecuteReader (); //Connection.Close (); return returnReader;}
/// /// Creates a DataSet by Running The Stored Procedure and Placing The resultS /// of the query / proc 通 t Given Tablename. /// summary> /// param> //// param> /// param> /// returns> protected DataSet Runprocedure ( string storedProcName, IDataParameter [] parameters, string tableName) {DataSet dataSet = new DataSet (); Connection.Open (); SqlDataAdapter sqlDA = new SqlDataAdapter (); sqlDA.SelectCommand = BuildQueryCommand (storedProcName, parameters); sqlDA.Fill (dataSet , TABLENAME); connection.close ();
Return DataSet;
/// ////////// of the stiled procedure. /// of the storedure. /// summary> /// param> /// param> /// param> /// param> / // returns> protected void RunProcedure (string storedProcName, IDataParameter [] parameters, DataSet dataSet, string tableName) {Connection.Open (); SqlDataAdapter sqlDA = new SqlDataAdapter (); sqlDA.SelectCommand = BuildIntCommand (storedProcName, Parameters; sqlda.fill (Dataset, Tablename); connection.close ();}}} coofucoo.core.bizObject: The business layer class has a base class, no content, it can be added later. Using system;
Namespace Coofucoo.core {/// /// The class from which all classes in the business tier /////////////////////////////////////////////////////-pUBLIC BIZOBJECT () {}}} Here to visit the previous article Click here to visit the next article