Hi, I have these codes to get the result from SQL table by using stored procedure which takes one paremeter (year),
Now, since I am using the entity framework, so I need to convert it to LINQ, basically, I need to read stored procedure with parameter (year) and get the result. Anybody knows how to convert it? thanks
conn.Open();
SqlDataReader myReader = null;
SqlCommand cmd = new SqlCommand("dbo.Business_budget", conn);
<[Function](Name:="sp_MyProcedure", IsComposable:=False)> _
Private Function GetData(<Parameter(Name:="Id", DbType:="int")> ByVal Id As Integer, _
<Parameter(Name:="name", DbType:="varchar(200)")> ByVal name As String) As ISingleResult(Of MyDataClass)
Dim _QueryResult As IExecuteResult = Nothing
If MyDataContext.DatabaseExists Then
_QueryResult = MyDataContext.ExecuteMethodCall(MyDataContext, CType(MethodInfo.GetCurrentMethod, MethodInfo), Id, name)
End If
Return CType(_QueryResult.ReturnValue, ISingleResult(Of MyDataClass))
End Function
<[Function](Name:="sp_MyProcedure", IsComposable:=False)> _
Private Function GetData(<Parameter(Name:="Id", DbType:="int")> ByVal Id As Integer, _
<Parameter(Name:="name", DbType:="varchar(200)")> ByVal name As String) As ISingleResult(Of MyDataClass)
Dim _QueryResult As IExecuteResult = Nothing
If MyDataContext.DatabaseExists Then
_QueryResult = MyDataContext.ExecuteMethodCall(MyDataContext, CType(MethodInfo.GetCurrentMethod, MethodInfo), Id, name)
End If
Return CType(_QueryResult.ReturnValue, ISingleResult(Of MyDataClass))
End Function
Peter Cong
Member
527 Points
681 Posts
How to convert this codes to LINQ?
Feb 21, 2013 02:32 PM|LINK
Hi, I have these codes to get the result from SQL table by using stored procedure which takes one paremeter (year),
Now, since I am using the entity framework, so I need to convert it to LINQ, basically, I need to read stored procedure with parameter (year) and get the result. Anybody knows how to convert it? thanks
conn.Open();
SqlDataReader myReader = null;
SqlCommand cmd = new SqlCommand("dbo.Business_budget", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@T_year", "2013"));
myReader = cmd.ExecuteReader();
myReader.Read();
if (myReader.HasRows)
{
BudgetAmt = Convert.ToDecimal(myReader["budget_amount"].ToString().Trim());
Anaksunamun
Contributor
2774 Points
515 Posts
Re: How to convert this codes to LINQ?
Feb 21, 2013 02:46 PM|LINK
see sample below
VB.NET
<[Function](Name:="sp_MyProcedure", IsComposable:=False)> _ Private Function GetData(<Parameter(Name:="Id", DbType:="int")> ByVal Id As Integer, _ <Parameter(Name:="name", DbType:="varchar(200)")> ByVal name As String) As ISingleResult(Of MyDataClass) Dim _QueryResult As IExecuteResult = Nothing If MyDataContext.DatabaseExists Then _QueryResult = MyDataContext.ExecuteMethodCall(MyDataContext, CType(MethodInfo.GetCurrentMethod, MethodInfo), Id, name) End If Return CType(_QueryResult.ReturnValue, ISingleResult(Of MyDataClass)) End FunctionC#
[Function(Name = "sp_MyProcedure", IsComposable = false)] private ISingleResult<MyDataClass> GetData([Parameter(Name = "Id", DbType = "int")] int Id, [Parameter(Name = "name", DbType = "varchar(200)")] string name) { IExecuteResult _QueryResult = null; if (MyDataContext.DatabaseExists) { _QueryResult = MyDataContext.ExecuteMethodCall(MyDataContext, (MethodInfo)MethodInfo.GetCurrentMethod, Id, name); } return (ISingleResult<MyDataClass>)_QueryResult.ReturnValue; }"Until Lions Have Their Historians, Tales of the Hunt Shall Always Glorify the Hunter"
Peter Cong
Member
527 Points
681 Posts
Re: How to convert this codes to LINQ?
Feb 21, 2013 03:02 PM|LINK
Hi, thanks a lot for your codes, it is hard to understand your codes, but I am trying to code something this:
var result = from c in db1.Business_budget() //db1 is the entity which contains the storedprocedure, here needs to accept the parameter - year.
where c.productid == "1234"
select c;
Please let me know if I am in the right direction.
Amy Peng - M...
Star
10123 Points
958 Posts
Microsoft
Re: How to convert this codes to LINQ?
Feb 27, 2013 05:13 AM|LINK
Hi,
"How to convert this codes to LINQ?"
I do not understand what do you want us to do with the code, could you please explain more?
Best Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store