Home  >  Article  >  Backend Development  >  Share the solution for invalid conversion of SqlDataReader

Share the solution for invalid conversion of SqlDataReader

零下一度
零下一度Original
2017-06-15 13:40:381228browse

This article mainly introduces in detail the solution to the invalid specified conversion of SqlDataReader, which has certain reference value. Interested friends can refer to

The solution to the invalid specified conversion of SqlDataReader. For details The content is as follows

//获取最新显示顺序数据
      string str = string.Format(@"if exists(select ShowOrder from GIS_FuncDefaultLayer 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())//判断当前功能Id下是否有数据
      {
        //读取赋值
        try
        {
          showOrder = dataReader.GetInt32(0);
          
        }
        catch (Exception ex)
        {

          HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(ex.Message) };
          return result;
        }
        
      }
dataReader.Close();//关闭

SqlDataReader's own GetInt32 (and other methods such as GerString, etc.)

just obtains the corresponding data in the database Columns of type do not have the function of type conversion, so they cannot be used like this

SolutionThere are two types

1. If you need to return int type, then the field in the database is defined as int type, then GetInt32 is feasible.
2. If the database is not defined as int type, and you want to return int type, then first Get it with the corresponding type in the database, and then convert it
such as int.Parse(selectunitidread.GetString(0))
If you don’t need to return the int type, just like what you wrote above, and finally convert it again For string
and the type in the database is varchar corresponding to string, then you can directly assign the following value
such as: rmoutbackinfo.UnitId = selectunitidread.GetString(0)
without converting to string

//解决
showOrder=int.Parse(dataReader.GetString(0));

The above is the detailed content of Share the solution for invalid conversion of SqlDataReader. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn