c# - Add date to sqlserver in yyyy-mm-dd -


when add lastimporteddate(dd-mm-yyyy) following method sql server fine. in database date yyyy-mm-dd

but add lastimporteddate(dd-mm-yyyy) different pc on same server day , month switched. in database date yyyy-dd-mm.

internal static void insertselloutsales(string customerid, string type, datetime lastimported, string periodstart, string periodend) {      // create sql connection #connection      sqlconnection sqlconnection1 = new sqlconnection(connection.connectionstring());      sqlcommand cmd = new sqlcommand();      cmd.commandtype = commandtype.text;       string periodstartquery = periodstart;      string periodendquery = periodend;       // create query values , execute query      if (!periodstart.equals("null"))      {          periodstartquery = " '" + periodstart + "'";      }       if (!periodend.equals("null"))      {          periodendquery = " '" + periodend + "'";      }       cmd.commandtext = "insert carssellout (customerid, type, lastimporteddate, periodstart, periodend) values ('" + customerid + "', '" + type + "', '" + lastimported + "', " + periodstartquery + ", " + periodendquery + ")";      cmd.connection = sqlconnection1;      sqlconnection1.open();      cmd.executenonquery();      sqlconnection1.close(); } 

note date settings on pc's both set dd-mm-yyyy.

if need more info please add comment.!

what can problem in case?

do not insert datetime values string representations. add datetime values directly parameterized queries.

sql server keeps datetime values in binary format. didn't have any format or something. saw them yyyy-mm-dd or dd-mm-yyyy textual representations.

generating different string representations of datetime instance different servers usually because use different culture settings. since didn't show relevant code generates strings, never know.

speaking of, should use parameterized queries. kind of string concatenations open sql injection attacks.

please read carefully;

as best practice, use using statement dispose connections , commands automatically instead of calling close methods manually.

using(var con = new sqlconnection(constring)) using(var cmd = con.cratecommand()) {     // define commandtext parameterized query.     // define parameters , values. add them add method command     // open connection     // execute query } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -