I have following SP in SQL Server
CREATE PROCEDURE [dbo].[SPGetData]
@StartDate DATETIME,
@EndDate DATETIME
AS
BEGIN
SELECT *
FROM SampleTable
WHERE CAST(SampleTable.CreateDate AS DATE) BETWEEN CAST(@StartDate AS DATE) AND CAST(@EndDate AS DATE)
END
END
GO
When I test this SP from Management Studio this is working fine , But I should Pass StartDate and EndDate values as following
DECLARE @return_value int
EXEC @return_value = [dbo].[SPGetStationData]
@StartDate = N'2016/10/08',
@EndDate = N'2016/10/09'
SELECT 'Return Value' = @return_value
GO
I want to send parameters from my back end c# program ,
So created method as following
public IEnumerable<ModelClass> GetDetails(DateTime? startdate, DateTime? enddate)
{
using (SqlConnection cnn = this.OpenConnection())
{
DynamicParameters parameters = new DynamicParameters();
parameters.Add("@StartDate", startdate);
parameters.Add("@EndDate", enddate);
IList<ModelClass> SampleList = SqlMapper.Query<ModelClass>(cnn, "SPGetData", parameters, commandType: CommandType.StoredProcedure).ToList();
cnn.Close();
return SampleList.ToList();
}
}
But when I debug this I can see values coming to this method as in Following format
in Stored Procedure asking YYYY/MM/DD
format but I have here in DD/MM/YYYY HH:MM:SS AM/PM
format
what should I do to do this to acceptable with SP asking format
My working code,
// Store procedure
CREATE PROCEDURE [dbo].[SPGetData]
@StartDate nvarchar(50) = null, -- change
@EndDate nvarchar(50) = null -- change
AS
BEGIN
-- change in where condition
SELECT *
FROM SampleTable
WHERE
DATEPART(yyyy,cast(SampleTable.CreateDate as date)) BETWEEN DATEPART(yyyy,cast(@StartDate as date)) and DATEPART(yyyy,cast(@EndDate as date))
END
END
GO
// Visual Studio Code for dapper
public IEnumerable<ModelClass> GetDetails(DateTime? startdate, DateTime? enddate)
{
var _para = new DynamicParameters();
_para.Add("@StartDate", startdate);
_para.Add("@EndDate", enddate);
var _list = _con.Query<ModelClass>("SPGetData", _para, commandType: CommandType.StoredProcedure); // _con is SqlConnection _con = new SqlConnection("your connection string")
return _list;
}
The following code works fine for me.
void Main()
{
DateTime? s=null;
DateTime? e=DateTime.Today;
Test(s,e);
}
void Test(DateTime? startDate, DateTime? endDate)
{
var cs=@"data source=(local);database=test;integrated security=true";
using (var con = new SqlConnection(cs))
{
con.Open();
var t = con.Query<string>("TestIt", new { startDate, endDate},
commandType: CommandType.StoredProcedure);
$"List: {t.FirstOrDefault()}".Dump();
}
}
Here is the procedure:
ALTER PROC TestIt
(
@startDate datetime2,
@endDate datetime2
)
AS
BEGIN
SELECT 'Start: ' + CASE WHEN @startDate IS NULL THEN 'Null' ELSE CONVERT(VARCHAR(35), @startDate, 106) END +
'\r\nEnd: ' + CASE WHEN @endDate IS NULL THEN 'Null' ELSE CONVERT(VARCHAR(35), @endDate, 106) END
END
Just note that unlike the code in comment I'm not defining any property names when passing parameters:
new { startDate, endDate}
As the procedure parameter names are same as the variable that I'm passing to the method. To use same code as in comment, you can modify it as this:
cnn.Query<ModelClass>("SPGetData",
new { StartDate=startDate??null, EndDate=endDate??null},
commandType:CommandType.StoredProcedure).ToList()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.