Link to home
Start Free TrialLog in
Avatar of setfocus
setfocus

asked on

DateTime With C# and MS SQL

Hello,

I seem to be missing something from this code, it always generates an incorrect syntax error.. how do I cast a C# DateTime to a MSSQL DateTime?

Thanks for you time
DateTime dateTime = new DateTime(2006, 09, 07, 22, 21, 44);
SqlCommand myCommand = new SqlCommand("SELECT * FROM Mytable1 WHERE MyDateAndTime = " + dateTime, myConnection);

 mySqlDataReader = myCommand.ExecuteReader();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I too agree with above suggestions, why do you want to invite hackers and SQL Injection? use parameter saw above.....
Avatar of setfocus
setfocus

ASKER

That's Excellent! Thanks very much for your time...
NP. Glad to help  :)