本文介绍了Enterprice Lib 5和SqlDataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在努力将Enterprise Lib 3.1升级到Enterprise Lib 5.0。我们有一个代码来获取/读取datareader中的数据,如下所示。    使用(SqlDataReader reader = Database.ExecuteReader(cmd)作为SqlDataReader) I am working on upgrading Enterprise Lib 3.1 to Enterprise Lib 5.0. We have a code to get/read data in datareader like below.  using (SqlDataReader reader = Database.ExecuteReader(cmd) as SqlDataReader)   {     if(reader.Read())   if (reader.Read())    {       int  leng = Convert.ToInt32(reader [0]);        byte []  value = reader [ 1] as byte [];       int leng = Convert.ToInt32(reader[0]);      byte[] value = reader[1] as byte[];        返回新的uHelperTemp(leng,value);       return new uHelperTemp(leng, value);     }    其他     {   else   {           返回null;         return null;      }     }    }    我将此更改为 如下所示它的工作正常。            ;   I change this to like below and it works fine.             使用 ( RefCountingDataReader reader =( RefCountingDataReader )Database.ExecuteReader(cmd))   {  {      if (reader.InnerReader.Read())       {      {         int leng = 转换 。ToInt32(reader.InnerReader [0]);        int leng =Convert.ToInt32(reader.InnerReader[0]);          byte [] value = reader.InnerReader [1] as byte [];         byte[]value = reader.InnerReader[1]asbyte[];          返回 new uHelperTemp(leng,value);         returnnew uHelperTemp(leng, value);        }       }        else        {       {            返回 null ;        }       }   }  } 然而,我正在研究这个问题,发现有些人正如下面所示。我想知道我上面的阅读是否有任何问题。 using(IDataReader iReader = DataObject.GetDataReader(sSql,cmd)) {  SqlDataReader reader =(SqlDataReader)((RefcountingDataReader) iReader).InnerReader; {  SqlDataReader reader = (SqlDataReader)((RefcountingDataReader)iReader).InnerReader; ...... } 推荐答案 我无法回答entlib中的更改,但它不应该真的很重要你把它投射到提供的内容:1)它是有效的,2)它是你真正关心的类型。除非你真的需要一个SqlDataReader或RefcountingDataReader或其他什么,然后只需单独留下类型。它返回一个IDataReader所以你可以使用它。I cannot answer to the changes in the entlib but it shouldn't really matter what you cast it to provided: 1) it is valid, 2) it is a type that you actually care about. Unless you really need a SqlDataReader or RefcountingDataReader or whatever then just leave the type alone. It returns an IDataReader so you can use that.using (var reader = Database.ExecuteReader(cmd)){ if (reader.Read()) { var leng = reader.GetInt32(0); var value = reader.GetValue(1) as byte[]; return new uHelperTemp(leng, value); };};如果你试图将它转换为其他任何东西,那么编译器会让它通过但是你依靠实现来返回一个与你的演员相匹配的类型。这通常不是你应该做的事情。If you try to cast it to anything else then the compiler will let it through but you're relying on the implementation to return a type that matches your cast. That is generally not something you should be doing. Michael Taylor http:/ /www.michaelt aylorp3.net Michael Taylorhttp://www.michaeltaylorp3.net 这篇关于Enterprice Lib 5和SqlDataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 12:51