How to make streams from BLOBs available in plain old C# objects when using SqlDataReader?

前端 未结 3 1466
小鲜肉
小鲜肉 2021-02-04 13:48

This is the scenario:

  • We store files, e.g. relatively large documents (10-300MB), in blobs in our MSSQL database.
  • We have a very small domain model so we
3条回答
  •  遥遥无期
    2021-02-04 14:27

    That's gorgeous! Thanks for this memory saver. Besides Marc's fix I modified the constructor to open connection and dispose in case the open or execute fails to reduce code/exception handling in caller. (Didn't know Dispose could be called from constructor). Constructor mod:

    try
    {
        this.command = command;     // store for disposal
    
        if (command.Connection.State != ConnectionState.Open)
            command.Connection.Open();
    
        dataReader = command.ExecuteReader(CommandBehavior.SequentialAccess);
        dataReader.Read();            
    }
    catch (Exception ex)
    {
        Dispose();
        throw;
    }
    

提交回复
热议问题