Пример #1
0
        /// <summary>
        /// Create a Stream for saving large object data from the server, use the
        /// stream attribute data
        /// </summary>
        /// <param name="format"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        internal static Stream CreateStream(StoredProcedureStreamOutputParamterAttribute format, object t)
        {
            var output = typeof(StoredProcedureStreamToFileParamterAttribute) == format.GetType() ?
                         ((StoredProcedureStreamToFileParamterAttribute)format).CreateStream(t) :
                         ((StoredProcedureStreamToMemoryParamterAttribute)format).CreateStream();

            // if buffering was requested, overlay bufferedstream on our stream
            if (format.Buffered)
            {
                output = new BufferedStream(output);
            }

            return(output);
        }
Пример #2
0
        internal static void ReadFromStream(this DbDataReader instance, object t, string name, PropertyInfo p, StoredProcedureStreamOutputParamterAttribute stream)
        {
            // handle streamed values
            var tostream = CreateStream(stream, t);

            try
            {
                using (var fromstream = instance.GetStream(instance.GetOrdinal(name)))
                {
                    fromstream.CopyTo(tostream);
                }

                // reset our stream position
                tostream.Seek(0, 0);

                // For array output, copy tostream to user's array and close stream since user will never see it
                if (p.PropertyType.Name.Contains("[]") || p.PropertyType.Name.Contains("Array"))
                {
                    var item = new Byte[tostream.Length];
                    tostream.Read(item, 0, (int)tostream.Length);
                    p.SetValue(t, item, null);
                    tostream.Close();
                }
                else if (p.PropertyType.Name.Contains("String"))
                {
                    var r    = new StreamReader(tostream, ((StoredProcedureStreamToMemoryParamterAttribute)stream).GetEncoding());
                    var text = r.ReadToEnd();
                    p.SetValue(t, text, null);
                    r.Close();
                }
                else if (p.PropertyType.Name.Contains("Stream"))
                {
                    // NOTE: User will have to close the stream if they don't tell us to close file streams!
                    if (typeof(StoredProcedureStreamToFileParamterAttribute) == stream.GetType() && !stream.LeaveStreamOpen)
                    {
                        tostream.Close();
                    }

                    // pass our created stream back to the user since they asked for a stream output
                    p.SetValue(t, tostream, null);
                }
                else
                {
                    throw new Exception(String.Format("Invalid property type for property {0}. Valid types are Stream, byte or character arrays and String",
                                                      p.Name));
                }
            }
            catch (Exception)
            {
                // always close the stream on exception
                tostream?.Close();

                // pass the exception on
                throw;
            }
        }