Petshop learning-sqlserverdal

Source: Internet
Author: User
I checked sqlserverdal at noon and drew a picture to post it first.
The SQL data access layer is designed for SQL Server databases. This layer is the bottom layer of the entire project!
Sqlhelper encapsulates data access functions through a set of static methods.
(The complete daab. Net sqlhelper code is printed out. After some comments are deleted, there are 21 pages in total. I don't want to attend classes .)
Sqlhelper. CS class is the base class of account. CS, inventory. CS, item. CS, order. CS, product. CS, profile. CS. All of them call the sqlhelper. CS class method for function operations.



(The figure is ugly ..)

Continue to check sqlhelper. CS of petshop ..

[1] sqlserverdal core --> sqlhelper. CS
Today, I wish you a smooth online job test !~~
Date: 2005-11-5

Continue reading...
Sqlhelper consists of the following static methods. Public static int executenonquery ()
Public static sqldatareader executereader ()
Public static object executescalar ()
Public static void cacheparameters ()
Public static sqlparameter [] getcachedparameters ()
Private static void PrepareCommand ()

First, let's take a look at PrepareCommand (). Because ExecuteNonQuery (), ExecuteReader (), and ExecuteScalar () All call PrepareCommand ()! Private static void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType primitive type, string plain text, SqlParameter [] partial parms)
{

// Whether the connection status is enabled
If (conn. State! = ConnectionState. Open)
Conn. Open ();

Cmd. Connection = conn;
Cmd. CommandText = plain text;

// Transact-SQL transaction
If (trans! = Null)
Cmd. Transaction = trans;

Cmd. CommandType = primitive type;

If (partition parms! = Null ){
Foreach (SqlParameter parm in milliseconds parms)
Cmd. Parameters. Add (parm );
}
}

PrepareCommand () has six parameters
SqlCommand cmd, // The SQL statement or stored procedure to be executed
SqlConnection conn, // database connection string
SqlTransaction trans, // Database Transaction
CommandType primitive type, // type of the execution database text or StoredProcedure
String plain text, // SQL statement (stored procedure and T-SQL command)
SqlParameter [] parameter parms // SqlCommand Parameter
PrepareCommand mainly determines whether the database status is enabled. If the status is disabled, the database is opened. Whether the Database Transaction trans is not empty. If not empty, the transaction operation is executed. In addition, some parameters are returned.

Let's take a look at CacheParameters (). In the SQLHelper class, we first declare a Private Static Hashtable domain, which is mainly used to store SQL parameters (partition parms ). It will be used in CacheParameters. Public static void CacheParameters (string cacheKey, // The identifier keyword of the buffer
Params SqlParameter [] partition parms) // SQL parameter Array
{
ParmCache [cacheKey] = parallel parms;
}

SQLHelper also defines a method for obtaining Cache parameters. GetCacheParameters () is used to obtain buffer SQL parameters from the Cache. Public static SqlParameter [] GetCachedParameters (string cacheKey ){
SqlParameter [] cachedParms = (SqlParameter []) parmCache [cacheKey];
// If the value is nll in the cache, a null value is returned.
If (cachedParms = null)
Return null;

SqlParameter [] clonedParms = new SqlParameter [cachedParms. Length];

// Copy the parameters in the Cache
For (int I = 0, j = cachedParms. Length; I <j; I ++)
ClonedParms [I] = (SqlParameter) (ICloneable) cachedParms [I]). Clone ();

Return clonedParms;
}

Let's take a look at the pre-processing method and then look at the three data reading methods ~
The main function of the ExecuteNonQuery () method is to return the affected rows. It has two reloads. The following lists all the reload methods of ExecuteNonQuery.
ExecuteNonQuery method public static int ExecuteNonQuery (string connString, CommandType limit type, string limit text, params SqlParameter [] limit parms)
{

SqlCommand cmd = new SqlCommand ();

Using (SqlConnection conn = new SqlConnection (connString )){
PrepareCommand (cmd, conn, null, partition type, plain text, plain parms );
Int val = cmd. ExecuteNonQuery ();
Cmd. Parameters. Clear ();
Return val;
}
}

The parameters are database connection strings, execution command types, SQL statements, and SQL parameter arrays.
The using (...) indicates that after the database operation is completed, the database connection is automatically closed and system resources are released.
ExecuteNonQuery method (reload 1) public static int ExecuteNonQuery (SqlConnection conn, CommandType limit type, string limit text, params SqlParameter [] limit parms)

ExecuteNonQuery method (reload 2) public static int ExecuteNonQuery (SqlTransaction trans, CommandType limit type, string limit text, params SqlParameter [] limit parms)

ExecuteReader method public static SqlDataReader ExecuteReader (string connString, CommandType primitive type, string plain text, params SqlParameter [] parallel parms)
{
SqlCommand cmd = new SqlCommand ();
SqlConnection conn = new SqlConnection (connString );
// We use a try/catch here because if the method throws an exception we want
// Close the connection throw code, because no datareader will exist, hence
// CommandBehaviour. CloseConnection will not work
Try {
PrepareCommand (cmd, conn, null, partition type, plain text, plain parms );
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 SQL parameter array. The main function is to provide a way to read only inbound row streams from the database.

There is also an executescalar method. This provides an overload. It is mainly used to return the row number objects that affect database records. Public static object executescalar (string connstring, commandtype limit type, string limit text, Params sqlparameter [] limit parms)

Executescalar overload public static object executescalar (sqlconnection Conn, commandtype primitive type, string plain text, Params sqlparameter [] parallel parms)

The SqlHelper. cs of PetShop is basically finished. In subsequent calls, SQLHelper. cs will also be returned for learning. First flash...

 

From: http://www.cnblogs.com/dotnet010/articles/958552.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.