开发者

Error converting data type bigint to int [SQL Server]

开发者 https://www.devze.com 2023-02-16 12:37 出处:网络
I have to insert a phone number into the database using stored procedure.. But while inserting it is showing the error as

I have to insert a phone number into the database using stored procedure.. But while inserting it is showing the error as

SqlException: Error converting data type bigint to int.

long phoneNumber = Convert.ToInt64(Phone.Text);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters开发者_Python百科.AddWithValue("@phone",phoneNumber);

cmd.ExecuteNonQuery();

ie for this parameter, DBType is Int64 SQLDbType is BigInt

I have used BigInt as the DataType of the column in SQL Server also.

So what could be going wrong here..

I find that 1999999999 [<2^32 ]is getting inserted properly and 2999999999 [>2^32]is not

Please tell if you need anymore info

Thanks in Advance


see this int, bigint, smallint, and tinyint

bigint
Integer (whole number) data from -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807). Storage size is 8 bytes.

int
Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647). Storage size is 4 bytes. The SQL-92 synonym for int is integer.

OP said:

I find that 1999999999 [<2^32 ]is getting inserted properly and 2999999999 [>2^32]is not

1,999,999,999 fits in an int because it is less than 2,147,483,647 but 2,999,999,999 does not because it is greater than 2,147,483,647.

Your best bet is to use a varchar(10) to store the phone number. If you have concerns about geting characters other than digits in your string, you can validate like this:

you can validate your string like this:

declare @PhoneNumber varchar(10)
select @PhoneNumber='123e0'
if @PhoneNumber LIKE '%[^0-9]%'
    print 'NOT NUMERIC'
else
    print 'NUMERIC'

select @PhoneNumber='1234567890'
if @PhoneNumber LIKE '%[^0-9]%'
    print 'NOT NUMERIC'
else
    print 'NUMERIC'

here is what you get when running the above:

NOT NUMERIC
NUMERIC


Use varchar(10) or char(10) instead of bigint.

0

精彩评论

暂无评论...
验证码 换一张
取 消