保存一个int到SQL,但保持前导零 [英] Storing an int to SQL, but keep leading zeros
问题描述
我在定义为int我的SQL Server 2012表中的字段。但是当我试图从一个文本框获得的价值在C#中使用转换(Convert.toint32(textbox.text))。比方说,如果文本框包含数字0032,它将被保存到数据库32取出00。
不是改变字段数据类型?? <其它任何解决方案/ p>
数字数据类型不保留前导零,因为他们是微不足道的,你想存储的号码。 char或varchar是比较合适的。您可以设置一个约束,以确保只有数字字符存储。
如果你绝对不能更改的数据类型,那么另一种选择是前导零的号码存入另一 INT
字段
因此,在您的例子中,你将存储:结果
值:32
前导零:2
I have a field in my SQL Server 2012 table defined as Int. but when I try to get the value from a textbox in C# using the converting (Convert.toint32(textbox.text)). Lets say if the textbox contains the number 0032, it will be saved to the database as 32 removing the 00.
Any solutions other than changing the Field Data Type??
Numeric datatypes do not retain leading zeros, as they are insignificant to the number you want to store. Char or Varchar is more appropriate. You could set a constraint to ensure only numeric characters are stored.
If you absolutely cannot change the data type, then another alternative is to store the number of leading zeros into another int
field
So in your example you would store:
Value : 32
Leading zeros : 2
这篇关于保存一个int到SQL,但保持前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!