简体   繁体   中英

How to pass Stored Procedure with DateTime parameter in Entity Framework

public List<Data> GetData(string Date1, string Date2, string param3)
{
    var myData= new List<FindData_Result>();
    using (var context = new Config())
    {
        var queryResult = context.FindData(startDate, endDate, param3);
        final= (from a in queryResult select a).ToList();
    }
}

How do I pass date1 and end date2 as datetime parameter to my stored procedure?

I am using MVC web api with entity framework (DB first approach)

You should convert the string representations of your dates, start date and end date, to their DateTime equivalent. This can be done by using the Parse method of DateTime type, like below:

var startDate = DateTime.Parse(Date1);
var endDate = DateTime.Parse(Date2);
var queryResult = context.FindData(startDate, endDate, param3).ToList();

You could also use the ParseExact method, provided that your string have a specific format. For further info please have a look here .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM