从Sring解析为整数值 [英] Parse From Sring to integer value

查看:94
本文介绍了从Sring解析为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我希望Text Box接受只有Integer值,所以制作了一个像文件框一样的文本框。现在我的问题是有没有必要解析文本框的值。

因为当我写的时候这个

Hi To All!! I want Text Box to accept Only Integer value so made one dll of text box like costume control.Now my question is that is there any need to parse the value of text box.
Because when i write like this

cmd.Parameters.Add(new SqlParameter("@PRollno", int.Parse(txtrolnum.Text)));



它给出错误输入字符串格式不正确



所以我这样做


it gives error "Input String not in a correct format"

so i did this

cmd.Parameters.Add(new SqlParameter("@PRollno", txtrolnum.Text));





现在程序运行正常,我只是想知道这背后的原因是什么。我在c#中比较新鲜。

感谢优先。

但是早期相同的代码运行得很完美。在后端我使用int数据类型并且不允许空值..



now program is working properly,I just want to know what is the reason behind this. I am fresher in c#.
thanks in advaance.
But earlier the same code were run perfectly.In backend i used int datatype and allow not null value..

推荐答案

如果Int.Parse返回输入字符串格式不正确,那么有一个字符(或多个字符)对于当前文化不正确PC运行粘贴操作。例如,文本框中的数字可以使用英国编号:1,234,567.89,其中PC文化设置为德语或希腊语:1.234.567,89



运行SQL服务器的计算机可能设置为不同的语言环境并正确解释它。



但为什么不让自定义控件返回数值而不是字符串?就像NumericUpDown控件一样吗?然后无论如何都不需要转换,并且会消除混淆。 (或者使用NumericUpDown控件)
If Int.Parse is returning "Input String not in a correct format", then there is a character (or characters) that are not correct for the current culture on the PC running the Paste operation. For example, the number in the textbox may use UK numbering: "1,234,567.89" where the PC culture is set to German or Greek: "1.234.567,89"

The computer running the SQL server may well be set to a different locale and interpret it correctly.

But why not have your custom control return a numeric value instead of a string? Just like the NumericUpDown control does? Then there is no need for conversion either way and confusion is removed. (Or use a NumericUpDown control instead)


当然你应该正确解析并注意正确的字符串格式,照顾你使用的文化,所有这些。



但我认为问题更深入,更简单。你怎么在数据库中存储一个字符串,其中语义要求具有数字类型?我不知道为什么,但看起来这几天一个问题确实困扰着初学者:尝试使用代表数据的字符串而不是数据本身。如果你想使用数字,请使用数字,真实,smallint,smallmoney等类型。



请参阅:http://msdn.microsoft.com/en-us/library/ms187594%28v=sql.90%29 .aspx [ ^ ]。



-SA
Of course you should parse properly and take care about proper string format, take care of the culture you use, all of that.

But I think the problem is deeper and simpler. How come you store a string in your database, where the semantic requires to have a numeric type? I don''t know why, but it looks like one problem really haunts the beginners these days: trying to work with strings representing data, instead of data itself. If you wan to work with numbers use the types like numeric, real, smallint, smallmoney and the like.

Please see: http://msdn.microsoft.com/en-us/library/ms187594%28v=sql.90%29.aspx[^].

—SA


这篇关于从Sring解析为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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