This article mainly for you to introduce the SqlDataReader specified conversion invalid solution, with a certain reference value, interested in small partners can refer to
SqlDataReader Specifies the resolution that the conversion is invalid, as follows
Gets the latest display order data for string str = string. Format (@ "If exists (select Showorder from gis_funcdefaultlayer where gisfuncid = {0}) select Max (showorder) as Showord Er from gis_funcdefaultlayer where gisfuncid ={0} Else select ' 0 ' as Showorder ", gisfuncid); IDataReader DataReader = helper. ExecuteReader (CommandType.Text, str); if (Datareader.read ())//Determine if the current function ID has data { //Read Assignment try { Showorder = datareader.getint32 (0); } catch (Exception ex) { Httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (ex. Message)}; return result; } } Datareader.close ();//Close
SqlDataReader GetInt32 (and other methods such as gerstring, etc.)
It just gets the columns of the corresponding data type in the database and does not have the function of type conversion, so it cannot be used
There are two ways of solving
1. If you need to return an int type, then the field in the database is defined as type int, then it is possible to use GetInt32
2. If the database does not define an int type and you want to return an int type, then use the corresponding type get out of the database and convert
such as Int. Parse (Selectunitidread. GetString (0))
If you do not need to return an int type, as you wrote above, the last is converted to string
And the type in the database is the varchar that corresponds to the string, so you can assign the following value directly
such as: Rmoutbackinfo. Unitid = Selectunitidread. GetString (0)
Instead of converting to string
Solve Showorder=int. Parse (datareader.getstring (0));