Create stored procedure and integrate it into linq to entities
2012-01-04 04:59
851 查看
/*by Jiangong SUN*/
Here I create a stored procedure for searching dealers in my database, it can receive several parameters.
Once the stored procedure is executed in the database. It's time to update the database model in linq to entities.
Firstly, open the entity data model, it's ended by .edmx or .edml. Click on model browser, the model will be displayed.
Right click on Stored procedure, then Update Model from Database,
You can see the tables, views and stored procedures, click the procedure i need, then it will be added in data model.
Once the stored procedure is added, Create a function import for it.
I need to choose a return type for the function import, here i choose the entity type object.
And then, I call the stored procedure in my project.
For now; the creation and integration of an stored procedure is finished.
I hope this article can do help to you!
Enjoy coding !!!
Here I create a stored procedure for searching dealers in my database, it can receive several parameters.
USE DATABASENAME; GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[GetDealers] @USER_X nvarchar(200), @USER_Y nvarchar(200), @COEF float, @RADIUS float, @COUNTRY nchar(2) AS BEGIN SELECT DealerId, RrdiId, Name, Address, Address2, ZipCode, City, FkCountry, GeoId, PhoneNumber, FaxNumber, Email, WebSite, OpenHours, Longitude, Latitude, Distance FROM ( SELECT DealerId, RrdiId, Name, Address, Address2, ZipCode, City, FkCountry, GeoId, PhoneNumber, FaxNumber, Email, WebSite, OpenHours, Longitude, Latitude, ( @COEF * acos(cos(radians(@USER_Y)) * cos(radians(Longitude)) * cos(radians(Cast(Replace(Latitude, ',', '.') as float)) - radians(@USER_X)) + sin(radians(@USER_Y)) * sin(radians(Cast(Replace(Longitude, ',', '.') as float)))) ) AS DISTANCE FROM dbo.Dealers WHERE (@COEF * acos( cos( radians(@USER_X) ) * cos( radians( Cast(Replace(Latitude, ',', '.') as float) ) ) * cos( radians( Cast(Replace(Longitude, ',', '.') as float) ) - radians(@USER_Y) ) + sin( radians (@USER_X) ) * sin( radians( Cast(Replace(Latitude, ',', '.') as float) ) ) ) ) < @RADIUS AND ISNUMERIC(Latitude) = 1 AND ISNUMERIC(Longitude) = 1 AND FkCountry = @COUNTRY ) t END
Once the stored procedure is executed in the database. It's time to update the database model in linq to entities.
Firstly, open the entity data model, it's ended by .edmx or .edml. Click on model browser, the model will be displayed.
Right click on Stored procedure, then Update Model from Database,
You can see the tables, views and stored procedures, click the procedure i need, then it will be added in data model.
Once the stored procedure is added, Create a function import for it.
I need to choose a return type for the function import, here i choose the entity type object.
And then, I call the stored procedure in my project.
/// <summary> /// Search dealers in database /// </summary> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <param name="coefficient"></param> /// <param name="radius"></param> /// <returns></returns> public List<Dealer> SearchDealers(string latitude, string longitude, int coefficient, int radius, Culture culture) { Log4net.Configure(); using (EntityConnection connection = new EntityConnection(this.Connection.ConnectionString)) { connection.Open(); try { using (EntityCommand command = connection.CreateCommand()) { List<Dealer> dealerList = new List<Dealer>(); Showroom_Entities context = new Showroom_Entities(); command.CommandText = "Entities.GetPdvDealers"; command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("USER_X", latitude); command.Parameters.AddWithValue("USER_Y", longitude); command.Parameters.AddWithValue("COEF", coefficient); command.Parameters.AddWithValue("RADIUS", radius); command.Parameters.AddWithValue("CULTURE", culture.CultureId); using (EntityDataReader edr = command.ExecuteReader(CommandBehavior.SequentialAccess)) { while (edr.Read()) { Dealer dealer = new Dealer { DealerId = Convert.ToInt32(edr[0]), RrdiId = edr[1].ToString(), Name = edr[2].ToString(), Address = edr[3].ToString(), ZipCode = edr[5].ToString(), City = edr[6].ToString(), GeoId = edr[9].ToString(), PhoneNumber = edr[10].ToString(), FaxNumber = edr[11].ToString(), Email = edr[12].ToString(), WebSite = edr[13].ToString(), OpenHours = edr[14].ToString(), Longitude = edr[15].ToString(), Latitude = edr[16].ToString(), Distance = edr[22].ToString(), }; dealer.Culture = (Culture)context.GetObjectByKey(culture.EntityKey); dealerList.Add(dealer); } } return dealerList; } } catch (Exception ex) { Log4net.Error("Search dealers error : " + ex.Message + Environment.NewLine + ex.StackTrace, ex); return null; } finally { connection.Close(); } } }
For now; the creation and integration of an stored procedure is finished.
I hope this article can do help to you!
Enjoy coding !!!
相关文章推荐
- How to Integrate SAP Business Data Into SharePoint 2010 Using Business Connectivity Services and LINQ to SAP
- 关于.ToList(): LINQ to Entities does not recognize the method ‘xxx’ method, and this method cannot be translated into a store expression.
- 报错: LINQ to Entities does not recognize the method...and this method cannot be translated into a sto
- LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression 解决办法
- How to Create the CAPL DLL and integrated it into CANOE
- create sqlserver login user and add it to certain db user (sqlserver authentication)
- How to write custom static code analysis rules and integrate them into Visual Studio 2010
- How to create a project from existing source in Eclipse and then find it?
- note : create zip and add file to it
- stored to (datesArray and programsDict) during its initialization is never read
- navicate how to create a stored procedure
- What is junction object and how to create it?
- Create a New SharePoint Permission Level and Bind it to an Existing SharePoint Group
- 53 Which statement about using RMAN stored scripts is true? A. To create and execute an RMA
- The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
- 如何为VS创建排序并移除不必要的命名空间引用-Is it possible to create a shortcut for removing unused usings into a class?
- How to write custom static code analysis rules and integrate them into Visual Studio 2010
- Magento. How to create an attribute and apply it to products
- Update on LINQ to SQL and LINQ to Entities Roadmap [ 关注 ]
- The specified type member 'IsLock' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.