Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Building a WhereClause with Date parameters.
Message
Information générale
Forum:
ASP.NET
Catégorie:
Web Services
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01207183
Message ID:
01207222
Vues:
14
the tables I'll be working on are using DateTime Fields. But I'll be sending just two dates and I would want them to be all encompassing dates. Should I then just add a time of 00:00:00 for the startdate and a time of 23:59:59 for the endtime? or is there a better way?

Hi, well, first, at a basic level,
DateTime StartDate = DateTime.Today.AddDays(-7);    // everything from a week ago, to today
DateTime EndDate = DateTime.Now;

string FilterExpr = 
  " WHERE OrderDate BETWEEN '" + StartDate.ToShortDateString() + "' AND '" + EndDate.ToShortDateString() + "'";
Basically, you have to stick a single quote inside the filter string, both before and after the date value.

Now, that code assumes that the dates themselves are stored just as dates (and times are 00:00). Note the use of the ShortDateString function.

However, if your data is stored in sql server as both date and time, then yes, you'd need to do somethign like you described with the ending time (construct something manually....like EndDate.ToShortDateString() + " 11:59:59 PM"

Hope that helps...
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform