SqlDataReader specifies the solution to invalid conversion, and the specified conversion is invalid

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.