CYQ. The path to Data lightweight Data layer use section 5 MProc stored procedure and SQL (16)

Source: Internet
Author: User

Previous Article: CYQ. Data lightweight Data layer road to use 4-song MAction addition, deletion, and modification (15th)

 

Summary

This article continues with the previous article. This section describes the operations related to the MProc class.
1: MProc Stored Procedure operations
2: MProc SQL Execution[ Supported in V1.5 or later versions]

 

I. Constructor

Method prototype:
Public MProc (object procNamesEnum)
Public MProc (object procName, string conn)

Note:

The constructor and MAction are used in the same way. Related operation parameters can be MAction.

Example 1:

// Input the stored procedure name. The default value is the database link of the configuration item Conn.
MProc proc = new MProc ("GetUserList ");
MProc proc = new MProc (ProcNames. GetUserList); // input through enumeration

Example 2:

// Input the new database link configuration item Conn.
MProc proc = new MProc ("GetUserList", "ChatConn ");
// Input the new database link to the direct link string
MProc proc = new MProc ("GetUserList", "Server =.; database = Chat; uid = sa; pwd = xxxxx ");
// Link with the agreed name in multiple databases from the ChatConn configuration item
MProc proc = new MProc (P_ChatEnum.GetUserList );

Example 3:

Note: This function is only supported in V1.5 and later versions.
// Execute the custom SQL statement. By default, it is taken from the connection of the Conn configuration item database.
MProc proc = new MProc ("select * from Users ");
// Execute the custom SQL statement from the ChatConn configuration item database link
MProc proc = new MProc ("select * from Users", "ChatConn ");

 

Ii. Parameter operations

Method prototype:

Public void Set (object paraName, object value)
Public void Set (object paraName, object value, SqlDbType sqlDbType)
Public void Clear ()

Example 1:

// Set parameters for stored procedures or parameterized SQL statements
Proc. Set ("ID", 888); // Add @ internally
Proc. Set ("@ ID", 888 );
Proc. Set (GetUserList. ID, 888); // pass through enumeration

Example 2:

Proc. Set (Users. ID, 888, SqlDbType. Int); // Add a parameter to the previous example.

Example 3:

Proc. Clear (); // Clear all Set parameters.

 

Iii. Method operations

Method prototype:

Public MDataTable ExeMDataTable ()
Public int ExeNonQuery ()
Public T ExeScalar <T> ()

Example 1:

MDataTable table = proc. ExeMDataTable (); // execution returns MDataTable

Example 2:

Int result = proc. ExeNonQuery (); // Add, update, and delete operations. The number of affected rows is returned.

Example 3:

String result = proc. ExeScalar <string> (); // run the statement to return the value of the first column in the first row.

 

 

Iv. Returned values and closed links

Method prototype:
Public int ReturnValue
Public void Close ()

Example 1:

MDataTable table = proc. ExeMDataTable ();
Int value = proc. ReturnValue; // if the execution is a stored procedure, the return value is obtained, which is the total number of records.

Example:

MProc proc = new MProc ("GetUserList ");
MDataTable table = proc. ExeMDataTable ();
Int value = proc. ReturnValue;
Proc. Close (); // Close the database link after execution

 

 

V. A complete example

Example:

Example:
Instantiation: MProc proc = new MProc (ProcNames. GetList );
Add parameter: proc. Set (GetList. ID, 10 );
Get list: MDataTable table = proc. ExeMDataTable (); Return Value: int count = proc. ReturnValue;
Close link: proc. Close ();
Bound control: GridView1.DataSource = table;
GridView1.DataBind ();

 

 

Statement:

This section describes how to use MProc to process Stored Procedure operations.
Some special custom SQL statement operations can also be performed [Requires V1.5 or later].

 

 

This tutorial ends now. The open-source V1.4 version will be available soon.

At the same time, if the relevant tutorials need to be upgraded, we will continue to introduce them.

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.