Exemplo n.º 1
0
		internal static List<DataReaderField> GetDataReaderFields(System.Data.IDataReader reader)
		{
			var columns = new List<DataReaderField>();

			for (var i = 0; i < reader.FieldCount; i++)
			{
				var column = new DataReaderField(i, reader.GetName(i), reader.GetFieldType(i));

				if (columns.SingleOrDefault(x => x.LowerName == column.LowerName) == null)
					columns.Add(column);
			}

			return columns;
		}
Exemplo n.º 2
0
        /// <summary>
        /// Generate CSV formatted output for the given reader.
        /// </summary>
        public string Generate(System.Data.Common.DbDataReader reader)
        {
            var builder = new StringBuilder();

            var schema = reader.GetSchemaTable();
            var colcount = reader.FieldCount;
            var nullable = new bool[colcount];
            var datatype = new Type[colcount];
            var typename = new string[colcount];

            for(int c=0; c<colcount; c++)
            {
                nullable[c] = true;
                datatype[c] = reader.GetFieldType(c);
                typename[c] = reader.GetDataTypeName(c);

                if (c == 0)
                {
                    if (this.Settings.AddLineNumbers)
                    {
                        if (this.Settings.QuotedStrings) builder.Append(this.Settings.StringQuote);
                        builder.Append("Line");
                        if (this.Settings.QuotedStrings) builder.Append(this.Settings.StringQuote);
                        builder.Append(this.Settings.FieldSeparator);
                    }
                }
                else
                {
                    builder.Append(this.Settings.FieldSeparator);
                }
                if (this.Settings.QuotedStrings) builder.Append(this.Settings.StringQuote);
                builder.Append(reader.GetName(c));
                if (this.Settings.QuotedStrings) builder.Append(this.Settings.StringQuote);
            }

            builder.Append(this.Settings.LineSeparator);

            var lineNumber = 0;
            while (reader.Read())
            {
                lineNumber++;

                for (int c = 0; c < colcount; c++)
                {
                    if (c == 0)
                    {
                        if (this.Settings.AddLineNumbers)
                        {
                            builder.Append(lineNumber);
                            builder.Append(this.Settings.FieldSeparator);
                        }
                    }
                    else
                    {
                        builder.Append(this.Settings.FieldSeparator);
                    }

                    if (nullable[c] && reader.IsDBNull(c))
                    {
                    }
                    else
                    {
                        if (datatype[c] == typeof(String))
                        {
                            if (this.Settings.QuotedStrings) builder.Append(this.Settings.StringQuote);
                            builder.Append(ToCsvableString(reader.GetString(c)));                            
                            if (this.Settings.QuotedStrings) builder.Append(this.Settings.StringQuote);
                        }
                        else if (datatype[c] == typeof(DateTime))
                        {
                            builder.Append(reader.GetDateTime(c).ToString(this.Settings.DateTimeFormat, this.Settings.FormatProvider));
                        }
                        else if (datatype[c] == typeof(Boolean))
                        {
                            builder.Append(reader.GetBoolean(c) ? this.Settings.BooleanTrue : this.Settings.BooleanFalse);
                        }
                        else
                        {
                            builder.AppendFormat(this.Settings.FormatProvider, "{0}", reader.GetValue(c));
                        }
                    }
                }

                builder.Append(this.Settings.LineSeparator);
            }

            return builder.ToString();
        }