c# - Unable to update current time in a excel using oledb -


i trying insert current time time column in excel using below code through oledb connection when check excel value inserted in date format.

value updated in excel - 1/0/1900 3:54:11 pm

expected value - 3:54:11 pm

         string currenttime = datetime.now.tostring("hh:mm:ss.fff tt");          string cmnd1 = "create table [" + currentdate + "] (testcase char(100), executiontime time, result char(20))";          string cmnd2 = "insert [" + currentdate + "] (testcase, executiontime, result) values ("+ "'" + tname + "',@dd,'" + result +"')" ;          using (oledbconnection conn = new oledbconnection(connectionstringtd))          {              oledbcommand createsheet = new oledbcommand(cmnd1, conn);              oledbcommand insertresult = new oledbcommand(cmnd2, conn);              insertresult.parameters.addwithvalue("@dd", datetime.now.timeofday);              conn.open();              try              {                  createsheet.executenonquery();              }              catch(oledbexception) {}              insertresult.executenonquery();          }       } 

afaik, when enter pure time value stored datetime entered time portion, , date part january 0, 1900 automatically since the days before 1900 incorrect in excel.

instead of that, pass datetime.now directly parameter , change column format type time h:mm:ss tt format in format cells part. way, paramterized @dd part. use parameters other values try insert. don't concatenate them.

insertresult.parameters.addwithvalue("@dd", datetime.now); 

enter image description here

and don't use addwithvalue anymore. it may generate unexpected , suprising results sometimes. use add method overload specify parameter type , it's size.

also use using statement dispose commands did connection.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -