Skip to content

Read data using DbDataReader Storeprocedure in Entityframework

February 16, 2011

How to use  store procedure outputparameter with Entity Framework?

Read data using DbDataReader Storeprocedure in Entityframework, we also see how to deal with Output parameter.

required namespace
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;

now look at the implementation, its very simple and saves lots of time.

public List SearchProduct1(string SearchText, string SearchBy, string Area, string countryId, int startRowIndex, int maximumRows, ref int TotalRows)
{
List< myProductInfo > objLst = new List< myProductInfo >();
int? _startRowIndex = startRowIndex;
int? _maximumRows = maximumRows;
int? _TotalRows = TotalRows;
int? _contryId = countryId == “” ? 0 : Convert.ToInt32(countryId);

try
{
using (var myEntities = new ApplicationEntities())
{
// this is how you can set the output parameter.
ObjectParameter param = new ObjectParameter(“TotalRows”, typeof(int));

/* here you probably wondering where the “SearchProduct” function come from, well open your edmx file and right click => import function, now select the sp you want to execute, then write the function name as your business need (ex. SearchProduct)
*/
objLst = myEntities.SearchProduct(SearchText, SearchBy, Area, _contryId, _startRowIndex, _maximumRows, param).ToList< myProductInfo >();

 TotalRows = Convert.ToInt32(pTotalRows.Value);
}
}

catch (Exception ex)
{
this.Status = ex.ToString();
}

return objLst;
}

Advertisement
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.