c# - How to store the date in dd/MM/yyyy in sql server? -
here code
datetime datetime = datetime.today; labeldate.text = datetime.tostring("'dd'/'mm'/'yyyy"); datetime d = datetime; sqlconnection con = new sqlconnection(); con.connectionstring= @"data source = desktop-u9dun78\sqlexpress;database = al_yousuf_db;integrated security=sspi"; con.open(); sqlcommand cmd = new sqlcommand("insert stockentrytb(date,product_no,product_name,quantity,original_cost,selling_cost)values('" + d + "'," + textboxprodno.text + ",'" + textboxprodname.text + "','" + textboxquantity.text + "'," + textboxoriginalcost.text + "," + textboxsellingcost.text + ")", con); cmd.executenonquery(); con.close();
but stored date in yyyy/dd/mm
format. 2016-06-11
(2016/june/11), want store 11/06/2016
.
stop that!
you have bad habit kick choosing wrong data type in sql server. should never store datetime
values string
.
in sql server, datetime
values stored binary. can see of format sql server shows you. under hood, doesn't have any implicit format.
that's why, should insert datetime database , datetime values database. "format" concept only applies when textual (string) representation of datetime values.
instead;
- define
date
columndatetime
ordatetime2
type. - insert
datetime
value directly parameterized query instead of string.
also use using
statement dispose connection , command automatically instead of calling close
or dispose
methods manually.
as last thing, date
might reserved keyword in future sql server versions, might wanna use [date]
instead. better solution, change column name non-reserved word.
Comments
Post a Comment