Using C# datetime in SQL where statement

Below is my code(C#), however nothing is displayed.

The problem seems to be with the @lastlogged parameter as if I hardcode its value are returned, the value lastlogged has already been converted using Convert.ToDateTime("2012-07-17 15:18:14.993").

Update Error: Must declare the scalar variable "@lastlogged".

string sqlStr = "SELECT * FROM View_AllJobDetails WHERE UpdatedDateStaff >= @lastlogged";
        SqlCommand cmd = new SqlCommand(sqlStr, cnn);
        cmd.Parameters.Add("@lastlogged", SqlDbType.DateTime);
        cmd.Parameters["@lastlogged"].Value = lastlogged;

Answers


Why don't you embed the lastlogged value itself into sqlStr -

string sqlStr = "SELECT * FROM View_AllJobDetails WHERE UpdatedDateStaff >= " + string.Format("{0:dd-MMM-yyyy}",lastlogged); 

Another approach could be creating a stored procedure in database and add this parameter.


I've noticed that sometimes parameters do have problems with dates. However, it can implicitly convert a string to a date. Therefore, try:

AddWithValue("@lastlogged", lastlogged.ToString)

Need Your Help

scons cannot import numpy module

python numpy scons

I have a sconstruct file and i am trying build a process.

Handle concurrent request in MS SQL for selecting data from a table randomly and updating the column of that particular row as used

sql-server hibernate web-services

We have one rest service which can be called by concurrent users and scenario where it is failing is when 2 parallel users are fetching same data from the table before user 1 update the flag