//public static BedTable ToBinaryTable(this IDataReader reader, int? maximumRecords)
        //{
        //    ITableStructure ts = reader.GetTableStructure(null);
        //    BedTable dt = new BedTable(ts);
        //    int allow_recs = maximumRecords != null ? maximumRecords.Value : -1;
        //    while (reader.Read() && (maximumRecords == null || allow_recs > 0))
        //    {
        //        dt.AddRow(reader);
        //        allow_recs--;
        //    }
        //    return dt;
        //}

        //public static BedTable ToBinaryTable(this IDataReader reader)
        //{
        //    return ToBinaryTable(reader, null);
        //}

        public static DataTable ToDataTable(this IDataReader reader, int?maximumRecords)
        {
            ITableStructure ts         = reader.GetTableStructure(null);
            DataTable       dt         = ConnTools.DataTableFromStructure(ts);
            int             allow_recs = maximumRecords != null ? maximumRecords.Value : -1;

            while (reader.Read() && (maximumRecords == null || allow_recs > 0))
            {
                DataRow row = dt.NewRow();
                for (int i = 0; i < ts.Columns.Count; i++)
                {
                    try
                    {
                        row[i] = reader[i];
                    }
                    catch (Exception)
                    {
                        row[i] = DBNull.Value;
                    }
                }
                dt.Rows.Add(row);
                allow_recs--;
            }
            return(dt);
        }
Exemple #2
0
        public static void WriteQueueToXml(IDataQueue queue, Stream fw, string dbName, string tableName)
        {
            DataTable table = ConnTools.DataTableFromStructure(queue.GetRowFormat);

            table.TableName = tableName;

            List <string> fldnames = new List <string>();

            foreach (IColumnStructure col in queue.GetRowFormat.Columns)
            {
                fldnames.Add(col.ColumnName);
            }

            XmlWriter xw = XmlTextWriter.Create(fw, new XmlWriterSettings {
                Encoding = Encoding.UTF8, CheckCharacters = false
            });

            //XmlTextWriter xw = new XmlTextWriter(fw, Encoding.UTF8);
            //xw.Settings = new XmlWriterSettings { CheckCharacters = false };

            xw.WriteStartDocument();
            xw.WriteStartElement(dbName);
            // mono has bug in writing schema
            if (!Core.IsMono)
            {
                table.WriteXmlSchema(xw);
            }
            List <string> ids = new List <string>();

            foreach (IColumnStructure col in queue.GetRowFormat.Columns)
            {
                ids.Add(XmlTool.NormalizeIdentifier(col.ColumnName));
            }
            try
            {
                while (!queue.IsEof)
                {
                    IBedRecord row = queue.GetRecord();
                    xw.WriteStartElement(tableName);
                    for (int i = 0; i < row.FieldCount; i++)
                    {
                        row.ReadValue(i);
                        var type = row.GetFieldType();
                        if (type == TypeStorage.Null)
                        {
                            continue;
                        }
                        xw.WriteElementString(ids[i], XmlTool.ObjectToString(row.GetValue(i)));
                    }
                    xw.WriteEndElement();
                }
            }
            finally
            {
                queue.CloseReading();
            }
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
        }