对于一个Int32[重复]来说,值要么太大,要么太小。

时间:2022-09-10 20:21:33

Possible Duplicate:
What is the maximum value for a int32?

可能重复:一个int32的最大值是多少?

Mobileno = Convert.ToInt32(txmobileno.Text);

error i amm getting while inserting in to database

在数据库中插入时出错。

3 个解决方案

#1


48  

Why on earth would you use an integer of any type to store a phone number?

为什么要使用任何类型的整数来存储电话号码呢?

You can't meaningfully do any arithmetics on one and you lose all leading zeroes.

你不能对一个数做任何运算你会失去所有的前导零。

Use a string instead.

使用一个字符串。

#2


10  

An integer (Int32) is limited in the values it can store since it "only" uses 32 bits. It can store a value between 2,147,483,647 and -2,147,483,648. (More information on MSDN)

整数(Int32)在它可以存储的值中是有限的,因为它“仅”使用32位。它可以存储2,147,483,647到-2,147,483,648之间的值。(MSDN的更多信息)

The value represented by the txmobileno.Text, is too large or too small.

由txmobileno表示的值。文本太大或太小。

Looking at the name txmobileno is probably a mobile phone number. This kind of numbers have too much digits to store in an int32. Also a phone number tends to start with a 0 or 00 or + (international). There's no way of storing this kind of information in an integer (or another number type). Just store them in a string.

看看这个名字,txmobileno可能是一个手机号码。这种数字有太多的数字,无法存储在int32中。还有一个电话号码往往以0或00或+(国际)开始。无法将此类信息存储在整数(或其他数字类型)中。将它们存储在一个字符串中。

#3


4  

As others have pointed out, storing a phone number as an integer is a mistake.

正如其他人指出的,将电话号码存储为整数是错误的。

  • You lose the ability to store characters and whitespace, for example country codes - "+44 (0800) 12345".
  • 您失去了存储字符和空格的能力,例如国家代码“+44(0800)12345”。
  • There is no logical reason to store it as an integer - would you ever need to do arithmetic on two telephone numbers? Does it make sense to add two phone numbers together?
  • 没有逻辑理由将它存储为整数——您是否需要对两个电话号码进行算术?把两个电话号码加在一起有意义吗?
  • Leading zeros will be lost - (0800 12345) will become (80012345).
  • 前导0将丢失——(0800 12345)将变成(80012345)。
  • Storing it as a string allows you to do regex validation on the user input.
  • 将其存储为字符串允许您对用户输入进行regex验证。

Having said that, the original question does raise some points which should be made:

说到这一点,最初的问题确实提出了一些应该提出的观点:

  • Prefer Int32.TryParse instead of Convert.ToInt32 when the source value is a string.
  • 喜欢Int32。TryParse而不是转换。当源值为字符串时,ToInt32。
  • When dealing with values which may potentially overflow - enclose the code in a checked { ... } block.
  • 当处理可能溢出的值时,将代码括在一个检查过的{…}。

#1


48  

Why on earth would you use an integer of any type to store a phone number?

为什么要使用任何类型的整数来存储电话号码呢?

You can't meaningfully do any arithmetics on one and you lose all leading zeroes.

你不能对一个数做任何运算你会失去所有的前导零。

Use a string instead.

使用一个字符串。

#2


10  

An integer (Int32) is limited in the values it can store since it "only" uses 32 bits. It can store a value between 2,147,483,647 and -2,147,483,648. (More information on MSDN)

整数(Int32)在它可以存储的值中是有限的,因为它“仅”使用32位。它可以存储2,147,483,647到-2,147,483,648之间的值。(MSDN的更多信息)

The value represented by the txmobileno.Text, is too large or too small.

由txmobileno表示的值。文本太大或太小。

Looking at the name txmobileno is probably a mobile phone number. This kind of numbers have too much digits to store in an int32. Also a phone number tends to start with a 0 or 00 or + (international). There's no way of storing this kind of information in an integer (or another number type). Just store them in a string.

看看这个名字,txmobileno可能是一个手机号码。这种数字有太多的数字,无法存储在int32中。还有一个电话号码往往以0或00或+(国际)开始。无法将此类信息存储在整数(或其他数字类型)中。将它们存储在一个字符串中。

#3


4  

As others have pointed out, storing a phone number as an integer is a mistake.

正如其他人指出的,将电话号码存储为整数是错误的。

  • You lose the ability to store characters and whitespace, for example country codes - "+44 (0800) 12345".
  • 您失去了存储字符和空格的能力,例如国家代码“+44(0800)12345”。
  • There is no logical reason to store it as an integer - would you ever need to do arithmetic on two telephone numbers? Does it make sense to add two phone numbers together?
  • 没有逻辑理由将它存储为整数——您是否需要对两个电话号码进行算术?把两个电话号码加在一起有意义吗?
  • Leading zeros will be lost - (0800 12345) will become (80012345).
  • 前导0将丢失——(0800 12345)将变成(80012345)。
  • Storing it as a string allows you to do regex validation on the user input.
  • 将其存储为字符串允许您对用户输入进行regex验证。

Having said that, the original question does raise some points which should be made:

说到这一点,最初的问题确实提出了一些应该提出的观点:

  • Prefer Int32.TryParse instead of Convert.ToInt32 when the source value is a string.
  • 喜欢Int32。TryParse而不是转换。当源值为字符串时,ToInt32。
  • When dealing with values which may potentially overflow - enclose the code in a checked { ... } block.
  • 当处理可能溢出的值时,将代码括在一个检查过的{…}。