views:

66

answers:

3

How do I pass C# DateTime values (FromCreateDate and ToCreateDate) to SQL Server 2005 in order to select from a view ?

The result set CreateDate column must be between FromDateDate and ToCreateDate.

+2  A: 

Exactly the same as you would any other parameter... of course it depends on how you are doing your data-access, but if we assume a SqlCommand you'd just refer to a named parameter (@fromDate / @toDate) in the TSQL, and add named SqlParameters (with .Value = theDate) to the command:

DateTime end = DateTime.Today, start = end.AddDays(-7); // the last week
using (var conn = new SqlConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.Parameters.AddWithValue("@from", start); // fine for DateTime; for strings, use more explicit param
    cmd.Parameters.AddWithValue("@to", end); // construction to avoid lots of different-length plans
    cmd.CommandText = "SELECT COUNT(1) FROM [Users] WHERE LastSeen >= @from AND LastSeen < @to";
    int count = (int) cmd.ExecuteScalar();
}

With LINQ etc you'd just use it in the query, i.e.

int count = db.Users.Count(user => user.LastSeen>=start && user.LastSeen<end);

I'm using a simple count in the examples just to keep it simple; obviously you can SELECT blah from a VIEW etc too...

Marc Gravell
@Mark: Do I must send two values as string and then convert them into datetime in stored procedure ?
odiseh
No; just set your `DateTime` variables into `yourParameter.Value`. I'll add an example...
Marc Gravell
+8  A: 

Just set the parameter type to SqlDbType.DateTime

e.g

SqlCommand cmd = new SqlCommand("Select * From dbo.MyView Where createDate = @FromDate", SqlCon);
cmd.Parameters.Add(new SqlParameter(@FromDate, SqlDbType.DateTime));
cmd.Parameters["@FromDate"].Value = fromDate;
Barry
A: 

If you are using Inline-Query :

private DataTable GetData(string StartDate,string EndDate)
{
string Cmd = SELECT * FROM DATE_SAMPLE WHERE createDate >= '"+StartDate+"' AND createDate <= '"+EndDate"'

SqlCommand SqlCmd - new SqlCommand (Cmd,SqlConnection);
SqlDataAdpater SqlAdpt = new SqlDataAdpater(SqlCmd);
DataTable Records = new DataTable();
SqlAdpt.Fill(Records);
return Records;
}

//Calling Function
GetData(dateTimePicker_StartDate.Value.ToString("MM/dd/yyyy"),dateTimePicker_End.Value.ToString("MM/dd/yyyy"));
KhanZeeshan
Especially for dates, with all the localization issues (m/d/y or d/m/y?), it's much better to use parameters.
Hans Kesting
i think to change the Format according to need its common sense but hey thats just me.
KhanZeeshan
In this day and age one should *never* build SQL queries by string concatenation when user input might ever be involved, in any but the most extremely simple of cases. And even then!
Andrew Barber
yeah thats right. but i gave this example so that even a newbie can understand.
KhanZeeshan
@KhanZeeshan: "common sense" date format for me is dd-MM-yyyy, not MM/dd/yyyy. And who knows what the system is set to where your app will run?
Hans Kesting
sql server can use yyyy-mm-dd which is the most clear in my opinion
KM
can anyone tell me was my answer wrong?i specifically wrote IF YOU ARE USING IN-LINE QUERY in the begging, Marc had already written the answer for LINQ so i wrote it for IN-LINE whats harm in that? Why are you guys taking it personally. Even the person who asked the question didn't had any problem with my answer then why are you guys so angry.
KhanZeeshan