Exemple #1
0
        internal static ILightDataTable ReadData(this ILightDataTable data, IDataReader reader, string primaryKey = null)
        {
            var i = 0;

            if (reader.FieldCount <= 0)
            {
                return(data);
            }
            data.TablePrimaryKey = primaryKey;

            var dataRowCollection = reader.GetSchemaTable()?.Rows;

            if (dataRowCollection != null)
            {
                foreach (DataRow item in dataRowCollection)
                {
                    //var isKey = Converter<bool>.Parse(item["IsKey"]);
                    var columnName = item["ColumnName"].ToString();
                    var dataType   = TypeByTypeAndDbIsNull(item["DataType"] as Type,
                                                           MethodHelper.ConvertValue <bool>(item["AllowDBNull"]));
                    if (data.Columns.ContainsKey(columnName))
                    {
                        columnName = columnName + i;
                    }
                    data.AddColumn(columnName, dataType);

                    i++;
                }
            }


            while (reader.Read())
            {
                var row = data.NewRow();
                reader.GetValues(row.ItemArray);
                data.AddRow(row);
            }

            reader.Close();
            reader.Dispose();
            return(data);
        }
Exemple #2
0
        internal static ILightDataTable ReadData(this ILightDataTable data, DataBaseTypes dbType, IDataReader reader, DbCommandExtended command, string primaryKey = null, bool closeReader = true)
        {
            var i = 0;

            if (reader.FieldCount <= 0)
            {
                return(data);
            }
            data.TablePrimaryKey = primaryKey;

            if (reader.FieldCount <= 0)
            {
                if (closeReader)
                {
                    reader.Close();
                    reader.Dispose();
                }
                return(data);
            }
            if (command.TableType == null)
            {
                try
                {
                    var key = command?.TableType != null ? command.TableType.FullName : command.Command.CommandText;
                    if (!CachedSqlException.ContainsKey(command.Command.CommandText))
                    {
                        if (!CachedGetSchemaTable.ContainsKey(key))
                        {
                            CachedGetSchemaTable.Add(key, new LightDataTable(reader.GetSchemaTable()));
                        }

                        foreach (var item in CachedGetSchemaTable[key].Rows)
                        {
                            var columnName = item.Value <string>("ColumnName");
                            data.TablePrimaryKey = data.TablePrimaryKey == null && item.Columns.ContainsKey("IsKey") && item.TryValueAndConvert <bool>("IsKey", false) ? columnName : data.TablePrimaryKey;
                            var dataType = TypeByTypeAndDbIsNull(item["DataType"] as Type,
                                                                 item.TryValueAndConvert <bool>("AllowDBNull", true));
                            if (data.Columns.ContainsKey(columnName))
                            {
                                columnName = columnName + i;
                            }
                            data.AddColumn(columnName, dataType);

                            i++;
                        }
                    }
                    else
                    {
                        for (var col = 0; col < reader.FieldCount; col++)
                        {
                            var columnName = reader.GetName(col);
                            var dataType   = TypeByTypeAndDbIsNull(reader.GetFieldType(col) as Type, true);
                            if (data.Columns.ContainsKey(columnName))
                            {
                                columnName = columnName + i;
                            }
                            data.AddColumn(columnName, dataType);
                            i++;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!string.IsNullOrEmpty(command.Command.CommandText))
                    {
                        CachedSqlException.Add(command.Command.CommandText, e);
                    }
                    return(ReadData(data, dbType, reader, command, primaryKey));
                }
            }
            else
            {
                foreach (var c in command.DataStructure.Columns.Values)
                {
                    data.AddColumn(c.ColumnName, c.DataType, c.DefaultValue);
                }
            }

            while (reader.Read())
            {
                var row = data.NewRow();
                reader.GetValues(row._itemArray);
                data.AddRow(row);
            }

            if (closeReader)
            {
                reader.Close();
                reader.Dispose();
            }

            return(data);
        }