保存String和int,保留任何前导零。 [英] Save String and int, retaining any leading zeros.

查看:124
本文介绍了保存String和int,保留任何前导零。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个ac#console应用程序正在从流中读取输入,其中一个变量是电话号码。


我正在抓取这些数据并将其保存为字符串。这很好用



不幸的是,SQL数据库我需要将此信息保存为此字段作为INT(不,我无法更改它) )。


我要做的是将字符串转换为INT,并保留任何前导零。



我使用这一行存储数据;

 string CALLEDNo = result.CALLDialed.Substring(3,11); //保存传入号,删除<>的

现在,如果我尝试使用

 int CALLERNOtoINT = Convert.ToInt16(CALLERNo); 

我收到错误


System.OverflowException :'对于Int16,价值太大或太小。'


所以,我尝试转换.ToInt32,并得到相同的消息。



如果我尝试

 long CALLERNOtoINT = long.Parse(CALLERNo); 




似乎满足"太大"的要求例外,&NBSP;但是当我尝试将其写入数据库时​​,我会收到以下消息。


System.OverflowException:'无法将参数值从Int64转换为Int32。'



请有人告诉我如何将字符串转换为整数,同时保留任何前导零?



谢谢


解决方案


int C#和SQL中的变量不能容纳大于2,147,483,647的数字。你想保存哪些电话号码?


为了保持前导零,也许你总是可以在数字之前添加一些数字,然后在从数据库读取之后将其删除。


Hello,

I have a c# console application that's is reading input from a stream, and one of the variables is the telephone number.

I'm grabbing this data and saving it as a string. That works fine

Unfortunately, the SQL database I need to save this information as this field as an INT (and no, I cannot change it).

What I am trying to do then is convert the string into an INT, and retain any leading zeros.

I am storing the data using this line;

string CALLEDNo = result.CALLDialed.Substring(3, 11);         // save incoming no, removing <>'s

Now, If I try and convert it to an int using

int CALLERNOtoINT = Convert.ToInt16(CALLERNo);

I get the error

System.OverflowException: 'Value was either too large or too small for an Int16.'

So, I try Convert.ToInt32, and get the same message.

If I try

long CALLERNOtoINT = long.Parse(CALLERNo);


that seems to satisfy the "too large" exception,  but I then get the following message when I try and write it to the Database.

System.OverflowException: 'Failed to convert parameter value from a Int64 to a Int32.'

Please, can someone advise me on how I can convert from a string to a Integer, whilst keeping any leading Zeros?

Thank you

解决方案

An int variable in C# and SQL cannot hold a number larger that 2,147,483,647. What telephone numbers are you trying to save?

In order to keep leading zeroes, maybe you can always add some digit before the number, then strip it after reading from database.


这篇关于保存String和int,保留任何前导零。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆