Database Component hxj. Data (8) (Stored Procedure)

Source: Internet
Author: User

The execution of SQL statements described in the previous section describes the execution of stored procedures.

The fromproc method is used to execute the stored procedure.

The execution of the parameter-free stored procedure is as follows:

DbSession.Default.FromProc("Ten Most Expensive Products").ToDataTable();

"Ten Most Expensive Products" is the name of the stored procedure.

 

Execute the stored procedure with parameters:

DbSession.Default.FromProc("Sales by Year")
.AddInParameter("Beginning_Date", DbType.DateTime, "1995-01-01")
.AddInParameter("Ending_Date", DbType.DateTime, "1996-12-01")
.ToDataTable();

This stored procedure in the database

create procedure "Sales by Year" 
@Beginning_Date DateTime, @Ending_Date DateTime AS
SELECT Orders.ShippedDate, Orders.OrderID, "Order Subtotals".Subtotal, DATENAME(yy,ShippedDate) AS Year
FROM Orders INNER JOIN "Order Subtotals" ON Orders.OrderID = "Order Subtotals".OrderID
WHERE Orders.ShippedDate Between @Beginning_Date And @Ending_Date

GO
There are two parameters: beginning_date and ending_date. The execution of stored procedures is similar to that of SQL statements. However, when stored procedures have more parameters, there will be input and output parameters. Add Input and Output Parameters Using addinputoutputparameter

The addoutparameter method adds the output parameter addreturnvalueparameter to the return parameter value. The example is as follows:
ProcSection proc = DbSession.Default.FromProc("testoutstore")     .AddInParameter("in1", System.Data.DbType.Int32, 1)     .AddOutParameter("out1", System.Data.DbType.Int32)     .AddOutParameter("out2", System.Data.DbType.String,100);proc.ExecuteNonQuery();Dictionary<string, object> returnValue = proc.GetReturnValues();foreach (KeyValuePair<string, object> kv in returnValue){    Response.Write("ParameterName:" + kv.Key + "    ;ReturnValue:" + Convert.ToString(kv.Value));    Response.Write("<br />");}

The getreturnvalues () method returns the returned value. The execution of stored procedures is also very simple. The next section describes other simple auxiliary methods of dbsession.

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.