How to execute a stored procedure in LINQ

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data. sqlclient;

Namespace XXXX. Data. helper
{
Public static class databaseextension
{
/// <Summary>
/// Execute the Stored Procedure
/// </Summary>
/// <Typeparam name = "telement"> </typeparam>
/// <Param name = "SP"> </param>
/// <Param name = "Parameters"> New sqlparameter () </param>
/// <Returns> </returns>
Public static int procedurequery (string sp, Params sqlparameter [] parameters)
{
Using (VAR context = new xxxxdbcontext ())
{
Using (context. database. Connection)
{
Context. database. Connection. open ();
VaR cmd = context. database. Connection. createcommand ();
Cmd. commandtext = sp;
Cmd. commandtype = commandtype. storedprocedure;
Foreach (VAR Param in parameters)
{
Cmd. Parameters. Add (PARAM );
}
Return cmd. executenonquery ();
}
}
}
/// <Summary>
/// Query the required data based on the Stored Procedure
/// </Summary>
/// <Typeparam name = "telement"> </typeparam>
/// <Param name = "SP"> </param>
/// <Param name = "Parameters"> </param>
/// <Returns> </returns>
Public static ienumerable <telement> procedurequery <telement> (string sp, Params object [] parameters)
{
String SQL = "";
List <telement> items = new list <telement> ();
Using (VAR context = new xxxxdbcontext ())
{
For (INT I = 0; I <parameters. length; I ++)
{
SQL + = "" + formatsqlstring (parameters [I]) + ",";
}
SQL = "EXEC" + SP + "" + SQL. trimend (',');
VaR tempresult = context. database. sqlquery <telement> (SQL, parameters );
If (tempresult! = NULL & tempresult. Count ()> 0)
{
Items. addrange (tempresult );
}
}
Return items;
}
Public static ienumerable <telement> procedurequery <telement> (string SP)
{
String SQL = "";
List <telement> items = new list <telement> ();
Using (VAR context = new xxxxdbcontext ())
{

SQL = "EXEC" + SP + "";
VaR tempresult = context. database. sqlquery <telement> (SQL );
If (tempresult! = NULL & tempresult. Count ()> 0)
{
Items. addrange (tempresult );
}
}
Return items;
}
Private Static string formatsqlstring (Object OBJ)
{
If (obj. GetType () = typeof (system. Boolean ))
{
Return (bool) OBJ? "1": "0 ");
}
If (obj. GetType () = typeof (system. guid ))
{
Return "'" + obj. tostring () + "'";
}
If (obj. getType () = typeof (system. int16) | obj. getType () = typeof (system. int32) | obj. getType () = typeof (system. int64) | obj. getType () = typeof (system. decimal) | obj. getType () = typeof (system. double ))
{
Return obj. tostring ();
}
Return "'" + obj. tostring () + "'";
}
}
}

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.