SqlDataReader specifies the solution to invalid conversion, and the specified conversion is invalid
SqlDataReader specifies the solution to the invalid conversion. The specific content is as follows:
// Obtain the latest display sequence data string str = string. format (@ "if exists (select ShowOrder from gis_funcdefalayer layer where GISFuncId = {0}) select max (ShowOrder) as ShowOrder from GIS_FuncDefaultLayer where GISFuncId = {0} else select '0' as ShowOrder ", GISFuncId); IDataReader dataReader = helper. executeReader (CommandType. text, str); if (dataReader. read () // determine whether data exists under the current function Id {// Read the value of try {showOrder = dataReader. getInt32 (0);} catch (Exception ex) {HttpResponseMessage result = new HttpResponseMessage {Content = new StringContent (ex. message)}; return result ;}} dataReader. close (); // Close
GetInt32 (and other methods such as GerString) that comes with SqlDataReader
It only obtains columns of the corresponding data type in the database and does not have the type conversion function.
SolutionThere are two types
1. If int type is returned, the fields in the database are defined as int type, and GetInt32 is applicable.
2. If the database does not define the int type and you want to return the int type, first Get the corresponding type in the database and then convert
For example, int. Parse (selectunitidread. GetString (0 ))
If you do not need to return the int type, as you wrote above, it is converted to a string.
The type in the database is the varchar corresponding to the string, so you can assign a value directly to the following
For example, rmoutbackinfo. UnitId = selectunitidread. GetString (0)
Instead of converting to string
// Solve showOrder = int. Parse (dataReader. GetString (0 ));
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.