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 () + "'";
}
}
}