C# .NET - selecting rows from table based on system date

Asked By raj on 03-Mar-14 08:04 AM
HI
i want to select some rows from my table based on the default system date or specific date . i am using c# with the back end sql server.

below my code which i tried, but not able to select the rows. i am in trouble

DateTime attdate = System.DateTime.Now;

sda = new SqlDataAdapter("select distinct empid from tbl_machineio where udate = '" + attdate + "' order by empid", conn);
 
i dont know how to solve this.

thanks in advance
Sathish S replied to raj on 04-Mar-14 01:18 AM

use queries with parameter instead of plain sql, which will prevent sql injections. Also check whether your store time in the database column specified. 

SqlCommand cmd = new SqlCommand("SELECT * FROM table_name WHERE dateTimeField = @SomeDate");

cmd.Parameters.AddWithValue("@SomeDate", DateTime.Today);

raj replied to Sathish S on 04-Mar-14 06:19 AM
Thanks for ur help. it works fine.