asp.net - Converting C# DateTime to T-SQL Time in SelectCommand -


i'm trying return query (in gridview in asp.net) time >= datetime.now.add(-60). where clause has been giving me no end of difficulties.

datetime pasttime = datetime.now.add(-60);     ds_db.selectcommand = "select * [vpurchasetotals] [timeoftransaction] >=  " + pasttime; 

my issue getting pasttime convert properly, returns newer data. [timeoftransaction] time(7) data type in table.

how parse c#'s datetime sql server's time?

here, try this:

using(sqlconnection conn = new sqlconnection(yourconnectionstring)) {     datetime pasttime = datetime.now.add(-60);       ds_db.selectcommand = @"select * [vpurchasetotals]                             [timeoftransaction] >= @pasttime";      sqlcommand cm = conn.createcommand();     cm.commandtext = ds_db.selectcommand;      cm.parameters.add("@pasttime", sqldbtype.time).value = pasttime.timeofday; //for comparison tsql time type      try     {         conn.open();         // need here.     }     catch(sqlexception e)     {         // handle exception     }         {         conn.close();     } } 

just future reference, should parameterize queries. ends being lot safer , cleaner/easier read , adjust.

edit: using sqldataadapter class? ds_db instance of? use string value query , implement sqldataadapter this:

try {      conn.open();       using(sqldataadapter da = new sqldataadapter(cm))      {           da.fill(datatable dt);      } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -