How can I convert String to Int?

后端 未结 30 2812
情歌与酒
情歌与酒 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:18

    You can convert string to int many different type methods in C#

    First one is mostly use :

    string test = "123";
    int x = Convert.ToInt16(test);
    

    if int value is higher you should use int32 type.

    Second one:

    int x = int.Parse(text);
    

    if you want to error check, you can use TryParse method. In below I add nullable type;

    int i=0;
    Int32.TryParse(text, out i) ? i : (int?)null);
    

    Enjoy your codes....

提交回复
热议问题