|
|
Question : How to compare a .NET Date variable with MSSQL DateTime Column?
|
|
I have a date variable that has exactly the same contents in my DateTime column in the database. However when I query the database, it doesn't return me any row.
Below is my code: Dim strdate As Date = CType("1/15/2003 5:40:45 PM", Date) Dim strSQL As String = SQL_STRING = "SELECT * FROM table WHERE dtLastModifiedDate='" & strdate & "'"
I've tried it out in the Query Analyzer, there is no error, but it just don't return me any row. I've also tried CONVERT and CAST, but to no avail.
Could someone tell me how to compare both?
|
Answer : How to compare a .NET Date variable with MSSQL DateTime Column?
|
|
Hello! Never use this way to pass parameters into SQL! Do it through the Parameters In your case: Dim strdate As Date = CType("1/15/2003 5:40:45 PM", Date) Dim strSQL As String = SQL_STRING = "SELECT * FROM table WHERE dtLastModifiedDate=[DT]" Set cmd = new ADODB.Command cmd.CommandText = strSQL cmd.Parameters.Append cmd.CreateParameter("DT", strdate) cmd.execute
You can use ADO.NET, then the syntax must be differ (as well as if you use ADO - did not used it a couple of months), but there should be parameters also.
Hope thiswill help
|
|
|
|
|