PETSHOP learning - SQLServerdal

xiaoxiao2021-04-04  235

I looked at SQLServerdal at noon and painted a picture.

SQL data access layer is designed for SQLServer databases. This layer is the bottom of the entire project!

SQLHELPER encapsulates the access function of data through a set of static methods.

(DAAB.NET SQLHELPER complete code is all printed, deleting a total of 21 pages of comments. Leave less less than a class.)

The SQLHELPER.CS class is an Account.cs, Inventory.cs, item.cs, orde.cs, product.cs, profile.cs six class base classes. They all call the SQLHELPER.CS class for functional operations.

Figure:

(Figure is a good ugly ..)

Continue to see Petshop's SQLHELPER.CS

..leave..

[1] SQLSERVERDAL core -> SQLHELPER.CS Today's soft test, Zhu Ge net test! ~~ Date: 2005-11-5 Continue to see ...

SQLHELPER consists of the following static methods. From:

public

Static

int

ExecutenonQuery ()

public

Static

SqlDataReader ExecuteReader ()

public

Static

Object

Executescalar ()

public

Static

Void

Cacheparameters ()

public

Static

Sqlparameter [] getCachedParameters ()

Private

Static

Void

PrepareCommand ()

Let's take a look at prepareCommand (). Because ExecuteNonQuery (), ExecuteReader (), ExecuteScalar () is called prepareCommand ()!

Private

Static

Void

PrepareCommand (SqlCommand CMD, SqlTransaction Conn, Sqltransaction Trans, CommandType CMDTYPE,

String

cmdtext, SQLParameter [] cmdparms)

{// Connection status Open if (Conn.State! = ConnectionsTate.Open) Conn (); cmd.connection = conn; cmd.commandtext = cmdText; // Transact-SQL Transaction IF (Trans! = NULL) CMD .Transaction = Trans; cmd.commandtype = cmdtype; if (cmdparms! = Null) {Foreach (Sqlparameter Parm in cmdparms) cmd.parameters.add (PARM);}}

PREPARCOMMAND () has 6 parameters

SQLCOMMAND CMD, // To execute SQL statement or stored procedure

SqlConnection conn, // database connection string

SQLTransaction Trans, // Database transaction

CommandType cmdtype, // Perform the type of the database text or storedprocedure

String cmdtext, // SQL statement (stored procedure, and t-sql command) SQLParameter [] cmdparms // sqlcommand parameters

PrepareCommand mainly determines if the database status is open, if the status is turned off, open the database. Database transaction TRANS is not empty, if it does not perform transaction operations for freeware. Alternately return some parameters.

Let's take a look at CacheParameters () first stated in the SQLHELPER class, the domain is primarily used to store the parameters of SQL (CMDPARMS). It will be used in cacheparameters ().

public

Static

Void

Cacheparameters

String

CacheKey,

//

Buffer logo keyword

Params

Sqlparameter [] cmdparms)

//

SQL parameter array

{PARMCACHE [CacheKey] = cmdparms;

SQLHELPER also defines a getting cache parameter method GetCacheParameters () for getting buffered SQL parameters from cache.

public

Static

Sqlparameter [] getCachedParameters

String

Cachekey)

{SqlParameter [] cachedParms = (SqlParameter []) parmCache [cacheKey]; // null if the cache if (cachedParms == null) return to nll return null; SqlParameter [] clonedParms = new SqlParameter [cachedParms.Length]; / / Copy the parameters in cache for (int i = 0, j = cachedparms.lend; i

Simply read the pre-processed method and then see the method of reading three data.

The main function of the executenonquery () method is to return the affected row. It has two overloads, and all of the overload methods of all ExecuteNonQuery are listed below.

ExecutenonQuery method

public

Static

int

ExecutenonQuery

String

Connstring, CommandType cmdtype,

String

cmdtext,

Params

Sqlparameter [] cmdparms)

{SqlCommand cmd = new SqlCommand (); using (SqlConnection conn = new SqlConnection (connString)) {PrepareCommand (cmd, conn, null, cmdType, cmdText, cmdParms); int val = cmd.ExecuteNonQuery (); cmd.Parameters.Clear (); Return val;}}

The parameter is in turn: Database connection string, execute command type, SQL statement, and an array of SQL parameters.

After that means indicates that the database operation is complete, the database connection is automatically turned off and releases system resources.

ExecutenonQuery method (overload one)

public

Static

int

ExecutenonQuery (SqlConnection CONN, CommandType CmdType,

String

cmdtext,

Params

Sqlparameter [] cmdparms)

ExecutenonQuery method (overloaded)

public

Static

int

ExecutenonQuery (SqlTransaction Trans, CommandType CmdType,

String

cmdtext,

Params

Sqlparameter [] cmdparms)

ExecuteReader method

public

Static

SqlDataReader ExecuteReader

String

Connstring, CommandType cmdtype,

String

cmdtext,

Params

Sqlparameter [] cmdparms)

{SqlCommand cmd = new SqlCommand (); SqlConnection conn = new SqlConnection (connString); // we use a try / catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist , hence the // commandBehaviour.CloseConnection will not work try {PrepareCommand (cmd, conn, null, cmdType, cmdText, cmdParms); SqlDataReader rdr = cmd.ExecuteReader (CommandBehavior.CloseConnection); cmd.Parameters.Clear (); return rdr ;} Catch {conn.close (); throw;}}

ExecuteReader contains four parameters: database connection string, database execution type, SQL statement, and an array of SQL parameter. The main role is to provide a way to read only the travel stream from the database.

Finally there is an ExecuteScalar method. This provides an overload. Its main purpose is to return to the number of rows that affect database records.

public

Static

Object

Executescalar

String

Connstring, CommandType cmdtype,

String

cmdtext,

Params

Sqlparameter [] cmdparms)

ExecuteScalar overload

public

Static

Object

ExecuteScalar (SqlConnection CONN, CommandType Cmdtype,

String

cmdtext,

Params

Sqlparameter [] cmdparms)

PETSHOP's SQLHELPER.CS is basically finished. In the back call, you will also return to learn SQLHELPER.CS. First flash ...

转载请注明原文地址:https://www.9cbs.com/read-131754.html

New Post(0)