c# - Using Date Difference to return value with datetime in an sqlserver from today to 7 days ago -


basically doing project connect sql , have return values have null 2 columns(tablein , tableout). need when program run checks today's date , goes 7 days , checks these 7 past days if there null value. checks column tabledate. managed connect database , display null values displaying whole list has null values. tried using date difference go 7 days today putting sql command see in code below. didn't work , i've been looking through internet nothing need coming , hoping if knew if me this. appreciated. code below.

using system; using system.collections.generic; using system.data.sqlclient; using system.linq; using system.text; using system.threading.tasks;  namespace sql_connection { class program    { static void main(string[]args) {       datetime olddate= datetime.now.adddays(-7);      datetime newdate = datetime.now;       timespan ts= newdate-olddate;      int diffferenceindays = ts.days;      string conn=null;     sqlconnection connection;     conn=("data source=database\\sql2012;initial catalog=table;user id=user;password=example");      connection=new sqlconnection(conn);      try{          connection.open();         console.writeline("connection open!");         sqlcommand cmd= new sqlcommand("select tabledate, tablein,tableout,tableuser [dbo].[tb_showingnull]"where dateadd(dd,-7,getdate()) tabledate , tablein , tableout null);         cmd.connection = connection;         sqldatareader reader = cmd.executereader();         while(reader.read())         {              console.writeline("{2}, {1} , {0}", reader["tablein"] == dbnull.value? "null" : reader["tablein"].tostring(), reader["tableout"] == dbnull.value? "null" : reader["tableout"].tostring(), reader["tableuser"].tostring(), reader["tabledate"].tostring();          }          connection.close();     }      catch(exception ex)     {        console.writeline(ex.message);     }      } } 

}

you sqlcommand query looks wrong. clause seems missing out of double quotes. think should this:

sqlcommand cmd= new sqlcommand("select tabledate, tablein,tableout,tableuser [dbo].[tb_showingnull] tabledate = dateadd(dd," + differenceindays + ",getdate()) , tablein null , tableout null"); 

you should note need specify whether 'tablein' should null well. writing 'tablein , tableout null' won't work filter on both of them being null. needs 'tablein null , tableout null'.


Comments