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