How can I convert String to Int?

后端 未结 30 2165
情歌与酒
情歌与酒 2020-11-21 05:35

I have a TextBoxD1.Text and I want to convert it to an int to store it in a database.

How can I do this?

30条回答
  •  天涯浪人
    2020-11-21 06:21

    int myInt = int.Parse(TextBoxD1.Text)
    

    Another way would be:

    bool isConvertible = false;
    int myInt = 0;
    
    isConvertible = int.TryParse(TextBoxD1.Text, out myInt);
    

    The difference between the two is that the first one would throw an exception if the value in your textbox can't be converted, whereas the second one would just return false.

提交回复
热议问题