Create stored procedure and integrate it into linq to entities

30 篇文章 0 订阅
15 篇文章 0 订阅

/*by Jiangong SUN*/

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 !!!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值