Beispiel #1
0
        /// <summary>
        /// Search all table names and columns names in Database
        /// </summary>
        /// <remarks>
        /// See https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/getschema-and-schema-collections
        /// </remarks>
        protected virtual IEnumerable <TableAndColumn> GetTablesDescription()
        {
            List <TableAndColumn> tableAndColumns = new List <TableAndColumn>();
            var fields = new SchemaColumnsFields(ProductName);

            // Columns
            DataTable allColumns = Connection.GetSchema(fields.NAME);

            // Tables et columns
            foreach (DataRow row in allColumns.Rows)
            {
                tableAndColumns.Add(new TableAndColumn()
                {
                    DatabaseFamily   = fields.DatabaseFamily,
                    SequenceNumber   = Convert.ToInt32(row[fields.SequenceNumber]),
                    SchemaName       = Convert.ToString(row[fields.SchemaName]),
                    TableName        = Convert.ToString(row[fields.TableName]),
                    ColumnName       = Convert.ToString(row[fields.ColumnName]),
                    ColumnType       = ExtractTypeNameOnly(Convert.ToString(row[fields.ColumnType])),
                    ColumnSize       = row[fields.ColumnSize] != DBNull.Value ? Convert.ToInt32(row[fields.ColumnSize]) : 0,
                    NumericPrecision = ConvertToNullableInt32(row[fields.NumericPrecision]),
                    NumericScale     = ConvertToNullableInt32(row[fields.NumericScale]),
                    IsColumnNullable = Convert.ToString(row[fields.IsColumnNullable]).ToBoolean(),
                });
            }

            return(tableAndColumns);
        }
Beispiel #2
0
        /// <summary>
        /// Search all table names and columns names in Database
        /// </summary>
        /// <remarks>
        /// See https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/getschema-and-schema-collections
        /// </remarks>
        protected virtual IEnumerable <TableAndColumn> GetTablesDescription(string onlySchema = null)
        {
            List <TableAndColumn> tableAndColumns = new List <TableAndColumn>();
            var fields = new SchemaColumnsFields(ProductName);

            // Columns
            DataTable allColumns;

            if (String.IsNullOrEmpty(onlySchema))
            {
                // All schema
                allColumns = Connection.GetSchema(fields.NAME);
            }
            else
            {
                // Only for a schema (for Oracle or SQL Server)
                string[] restrictionsValues;

                if (fields.DatabaseFamily == DatabaseFamily.Oracle)
                {
                    restrictionsValues = new string[] { onlySchema, null, null }
                }
                ;
                else
                {
                    restrictionsValues = new string[] { null, onlySchema, null }
                };

                allColumns = Connection.GetSchema(fields.NAME, restrictionsValues);
            }

            // Tables et columns
            foreach (DataRow row in allColumns.Rows)
            {
                tableAndColumns.Add(new TableAndColumn()
                {
                    DatabaseFamily   = fields.DatabaseFamily,
                    SequenceNumber   = Convert.ToInt32(row[fields.SequenceNumber]),
                    SchemaName       = Convert.ToString(row[fields.SchemaName]),
                    TableName        = Convert.ToString(row[fields.TableName]),
                    ColumnName       = Convert.ToString(row[fields.ColumnName]),
                    ColumnType       = ExtractTypeNameOnly(Convert.ToString(row[fields.ColumnType])),
                    ColumnSize       = row[fields.ColumnSize] != DBNull.Value ? Convert.ToInt32(row[fields.ColumnSize]) : 0,
                    NumericPrecision = ConvertToNullableInt32(row[fields.NumericPrecision]),
                    NumericScale     = ConvertToNullableInt32(row[fields.NumericScale]),
                    IsColumnNullable = Convert.ToString(row[fields.IsColumnNullable]).ToBoolean(),
                });
            }

            return(tableAndColumns);
        }