Date comparison with C# and SQL Server 2005

rhaazy's Avatar, Join Date: Jul 2007
Go4Expert Member
In C# code I want to do a date comparison against a column is the database.

Something like:

select someColumn from someTable
where CurrentDate-someDate > ElapsedTimeColumn

I want to get the currentdate in C# and run a query from my application.

How do i get the format of:

mm/dd/yyyy hh:ss:mm AM/PM

shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
DateTime class located in the System namespace would help you.


rhaazy's Avatar, Join Date: Jul 2007
Go4Expert Member
Any idea what would throw an sql exception error?
The format it returns is identical to the format of my sql column....

This isn't an issue, and I decided to just use the sql function getdate to do what I needed, but I do no understand why this would throw an error. Perhaps it is a simple formatting error on my part when concatenating the strings, but maybe someone had a similiar problem before. thanks.
SNK111's Avatar
Newbie Member
This scenario usually arises when the data is not 'received' properly from the database.

Check this link ( ) for details on how to connect to SQL Server database from C#.NET database applications as well as Java database applications. It also describes how to pass embedded SQL queries, calling stored procedures, pass parameters etc.

Shahriar Nour Khondokar: