Date comparison with C# and SQL Server 2005

Discussion in 'C#' started by rhaazy, Aug 3, 2007.

  1. rhaazy

    rhaazy New Member

    Joined:
    Jul 6, 2007
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    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

    Thanks.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,374
    Likes Received:
    388
    Trophy Points:
    83
    DateTime class located in the System namespace would help you.

    Code:
    System.DateTime.Now
    
    System.DateTime.Today
     
  3. rhaazy

    rhaazy New Member

    Joined:
    Jul 6, 2007
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Any idea what system.datetime.now 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.
     
  4. SNK111

    SNK111 New Member

    Joined:
    Apr 10, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    This scenario usually arises when the data is not 'received' properly from the database.

    Check this link ( shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html ) 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: shahriarnk.com/
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice