How to add parameters in a SQL select query?
string time = 2013-09-25 00:00:00;
I wish to use the time
variable in the below mentioned SQL query
Select LastUpdated from Employee where LastUpdated > time;
How to add parameters in a SQL select query?
string time = 2013-09-25 00:00:00;
I wish to use the time
variable in the below mentioned SQL query
Select LastUpdated from Employee where LastUpdated > time;
Try this:
string sqlDate = time.ToString("yyyy-MM-dd HH:mm:ss.fff");
You can convert your string in C# code to DateTime using
DateTime.TryParse()
or Convert.ToDateTime()
OR
Convert VARCHAR to DATETIME in SQL using
Convert(datetime, time)
It appears what the OP is asking is how to convert a VARCHAR
to a DATETIME
in SQL Server not actually a String
to DateTime
in C#.
You will need to use the following to convert to a DATETIME
:
SELECT LastUpdated
FROM Employee
WHERE LastUpdated > CONVERT(datetime, varTime, 121);
See the following MS Reference for more information.
To echo others though, you should just pass the parameter in as a datetime and let the database provider factory handle the conversion of appropriate types, or add a new method that actually returns a DateTime
. In the future, I wouldn't name a method GetUpdateTime
unless it actually returns a type of Time
.
I just framed my question in a wrong, the query remains the same though. I just wanted time
to be added as a paramter in my SQL-query. The code for the same looks like
String commandText = "Select LastUpdated from Employee where LastUpdated > :time;";
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
command.Parameters.Add("time", time);
Thanks a lot for your help! My bad that I couldn't frame the question properly.