c# – 使用继承的自定义IDataReader从IDbCommand读取

前端之家收集整理的这篇文章主要介绍了c# – 使用继承的自定义IDataReader从IDbCommand读取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经使一个自定义类继承IDataReader并且已经成功
实现了一个使用C#对象而不是DataTable的自定义类的自定义ServerWriter sqlBulkCopy.

这被证明是一个更有效的方法,我怀疑.

现在我想读,我有一些问题

这是IDataReader:

// get Server Data Table Object IDataReader
public class GetSDTOIDataReaderM : IDataReader
{
   //private IEnumerator<MyTestObject> enumerator = null;

   public MysqLDbTableObject.Raw.sqlDbTableM propsqlTbl { get; set; }

   // implementing relevant GetValue(),filedCount() and Read()
   // this is how I did with WriteToServer
   public bool Read()
   {
       return ++this.ReaderRowsCount < this.propsqlTbl.Table.ElementAt(0).Row.Count;
   }
}

该对象与其相应的sqlTable精确映射.

在我的自定义IDataReader中设置一个sqlCommand到ExecuteReader()没有工作,所以我试过
IDbCommand然后它编译,但仍然在尝试阅读
我收到一个错误

Unable to cast object of type ‘System.Data.sqlClient.sqlDataReader’ to
type ‘GetSDTOIDataReaderM’

this.propComunicator.AsGetSDTOCom().sqlCmd.Connection.Open();
//this is the line of the error
using (this.propComunicator.AsGetSDTOCom().SDTOIDataReader = (Raw.Comunicator.Raw.GetSDTOIDataReaderM)this.propComunicator.AsGetSDTOCom().sqlCmd.ExecuteReader())
{

}

写入服务器它的工作,我怎么能正确实现与读?

UPDATE

更多关于我的模块

public class TransManSF
{
    public enum CommunicatorTypeS { Reader,Adapter,GetSDTOReaderA,sqlBcpyIDataReaderM };
    public enum DbIntreactorTypeS { TableObject,TableRowObject,Sproc,sqlCmd };
    public enum sqlCmdActionS { NotSet,SELECT,INSERT,UPDATE,DROP };
    public enum sqlCmdStrSelModeS { Single,RecentEfected,count,All,Top10,Top100,Top1000 };
    public enum sqlCmdStrSelOrderByS { NotSet = 0,Asc = 1,Desc = 2 };
    public enum sqlCmdStrSetterModeS { Single,Multi};
    public enum STDOTypeS { NotSet,ServerWriter,ServerTableGetter,ServerBiDerctional }
    public enum SprocTypeS { Sproc,TvPSrpoc }
    public enum TransExecActionTypeS { WriteToServer,GetTable }
}

public static TransMan.Raw.ComunicatorCLF.BulkCopyComSCL AsBulkCopyCom(this TransMan.Raw.ComunicatorCLF.ComunicatorM SelfCom)
{
    return (TransMan.Raw.ComunicatorCLF.BulkCopyComSCL)SelfCom;
}

virtual public void ExecuteTransaction(AppMods.DataBase.TransManSF.TransExecActionTypeS parSelectedTransType,TransManSF.sqlCmdStrSelOrderByS parExecOrderByS,int OrderByOrdinal = 0)
{
    if (parSelectedTransType == AppMods.DataBase.TransManSF.TransExecActionTypeS.WriteToServer)
        using (this.propComunicator.AsBulkCopyCom().Conn)
        {
            this.propComunicator.AsBulkCopyCom().Conn.Open();
            using (var IDRdrServerWriter = this.propComunicator.AsBulkCopyCom().ServerWriter)
            {
                var Eng = this.propExecuter.AsBulkCopyExec().Engine;
                Eng.BulkCopyTimeout = 240;
                Eng.WriteToServer(IDRdrServerWriter);

            }
            this.propComunicator.AsBulkCopyCom().Conn.Close();
        }
    else if (parSelectedTransType == AppMods.DataBase.TransManSF.TransExecActionTypeS.GetTable)
    {
        var DreaderCom = this.propComunicator.AsDReaderCom();
        using (DreaderCom.sqlCmd.Connection)
        {
            DreaderCom.sqlCmd.Connection.Open();
            using (DreaderCom.Driver = DreaderCom.sqlCmd.ExecuteReader())
            {
                if (DreaderCom.Driver.HasRows) while (DreaderCom.Driver.Read())
                    {
                        for (int i = 0; i < DreaderCom.Driver.FieldCount; i++)
                        {
                            var CurRdrColumn = DreaderCom.Driver.GetValue(i);
                            this.Table[i,0] = CurRdrColumn;
                        }
                    }
            }
            DreaderCom.sqlCmd.Connection.Close();
        }                           
    }
}


public struct customComConfgBulkCopyA
{
      public TransManSF.CommunicatorTypeS ComType;
      public customComConfgBulkCopyA(TransManSF.CommunicatorTypeS ctrComType = TransManSF.CommunicatorTypeS.sqlBcpyIDataReaderM)
      {
          this.ComType = ctrComType;
      }
}
public sealed class BulkCopyComSCL :CustomComA
{
     public new Configurations.comunicator.customComConfgBulkCopyA Meta;
     public sqlConnection Conn { get; set; }
     public Raw.sqlBcpyIDataReaderM ServerWriter { get; set; }
}

public class sqlDbTableM : sqlDB1stTransA
{
     virtual public DbSchema.Raw.TableDictionaryA TableDict { get; set; }
     public virtual new TransMan.Raw.Configurations.SDB1stTransConfgF.SDTOMetaA Meta { get; set; }
     virtual public Raw.ColumnSetsCollM Table { get; set; }
     public override TransMan.Raw.ComunicatorCLF.ComunicatorM propComunicator
     {
         get
         {
              return base.propComunicator;
         }
         set
         {
              base.propComunicator = value;
         }
     }
     public override TransMan.Raw.Executers.ExecuterM propExecuter
     {
         get
         {
             return base.propExecuter;
         }
         set
         {
             base.propExecuter = value;
         }
     }
     public sqlDbTableM(TransManSF.STDOTypeS ctrTransType)
            : base(TransManSF.DbIntreactorTypeS.TableObject) { }
}

public sealed class GetSDTOComSCL : CustomComA
{
    public new Configurations.comunicator.customComConfgGetSDTOReaderA Meta;
    public new IDbCommand sqlCmd;
    public Raw.GetSDTOIDataReaderM SDTOIDataReader { get; set; }
}

解决方法

简短的答案:您不能更改sqlCommand.ExecuteReader()返回的类型.

它将始终是sqlDataReader的一个实例.

各种解决方

1把你所了解的东西包裹起来.

如果你去极端,写自己的IDbProviderFactory,它包装一个sqlConnection与自己的连接,命令和datareader类.

例如,这就是Miniprofiler对所有sql请求进行测试,并收集执行时间,行数等

或者您可以使用某种扩展方法来包装读取器,如:sqlCmd.ExecuteReader().AsMyReader().什么AsMyReader()会做的是返回一个自定义IDataReader,它的东西和委托到实际的sqlDataReader.

2使用现有的助手库

我不太清楚你在阅读内容时想要实现什么,但是在这个空间中存在着很好的图书馆.没有必要提出自己的解决方案.

FastMember(Nuget)允许在任何IEnumerable< T>之间创建IDataReader接口.或DataTable.它被轻易地用作sqlBulkCopy的种子,以便最大程度地插入数据.性能进入sql Server表.

此外,为了简化执行SQL查询并解析其结果,@L_403_2@非常棒.

原文链接:https://www.f2er.com/csharp/95475.html

猜你在找的C#相关文章