简体   繁体   中英

Passing DateTime in the where class as a parameter C#

I am trying to pass two DateTime parameters to an SQL statement. This is what I am trying to pass along with the C# code:

string command = @"SELECT EMP._name AS employername, EMP._contact AS employercontact, EMP.employerid, EMP.firmid 
                 FROM EMPLOYER AS EMP 
                 LEFT JOIN SERVICE AS SERV 
                 ON EMP.employerid = SERV.employerid 
                 WHERE SERV._dateRecordCreated >= @yearStartDate 
                 AND SERV._dateRecordCreated  <= @yearEndDate 
                 ORDER BY employername;";

And this is how I am passing the paramaters:

DateTime yearStartDt = new DateTime(2015, 1,1);
DateTime yearEndDt = new DateTime(2015, 12, 31);

sqlCommand.Parameters.Add("@yearStartDate", yearStartDt);
sqlCommand.Parameters.Add("@yearEndDate", yearEndDt);

I keep getting SQL Exception 137

use something like this

sqlCommand.Parameters.Add("@yearStartDate", SqlDbType.DateTime);
sqlCommand.Parameters["@yearStartDate"].Value = yearStartDt;

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