SqlDataReader指定转换无效的处理要领,具体内容以下
//猎取最新显现递次数据 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 自带的GetInt32(以及其他的比方GerString等)要领
只是猎取数据库中对应数据范例的列,并不具有范例转化的功用,所以不能如许运用
处理要领有两种
1.如须要返回int范例的,那末数据库中的字段就定义为int范例,则用GetInt32可行
2.若数据库中定义的不是int范例,又想要返回int范例的,那末就先用数据库中对应的范例Get出来,然后转化
如int.Parse(selectunitidread.GetString(0))
假如不须要返回int范例的,就如你上面写的到的,末了是又转化为了string
而数据库中的范例就是与string对应的varchar,那末你能够直接给下面的赋值
如:rmoutbackinfo.UnitId = selectunitidread.GetString(0)
而不必再转化为string
//处理 showOrder=int.Parse(dataReader.GetString(0));
以上就是圆满处理SqlDataReader指定转换无效的细致内容,更多请关注ki4网别的相干文章!