When processing the database, it is usually necessary to convert the string value from the input form or the user input to an integer to store it. This can be implemented using the
or method. Int32.Parse
Int32.TryParse
The method converts the specified string to an integer. If the conversion is successful, it will return an integer; otherwise, it will trigger abnormal.
grammar: Int32.Parse
FormatException
Example:
int x = Int32.Parse(string);
<> Try to convert the specified string into an integer. If the conversion is successful, it will return
and assign an integer value to the output parameter; if the conversion fails, it will returnint x = Int32.Parse(TextBoxD1.Text);
<法> grammar:
Int32.TryParse
<示> Example: true
false
int x; bool result = Int32.TryParse(string, out x);
will throw abnormalities when failed, and returns and will not throw an exception. This allows more elegantly to deal with conversion attempts.
The above is the detailed content of How to Safely Convert Strings to Integers for Database Storage?. For more information, please follow other related articles on the PHP Chinese website!