Пример #1
0
        public ObjectData GetObjectDataFromTable(string tableName)
        {
            var result = new ObjectData(tableName);

            string cmdText = "select t.COLUMN_NAME, t.DATA_TYPE, cons.CONSTRAINT_TYPE " +
                             "from USER_TAB_COLUMNS t " +
                             "left join all_cons_columns cols " +
                             "on t.TABLE_NAME = cols.TABLE_NAME " +
                             "and t.COLUMN_NAME = cols.COLUMN_NAME " +
                             "left join all_constraints cons " +
                             "on cols.CONSTRAINT_NAME = cons.CONSTRAINT_NAME " +
                             "where t.table_name = :tableName " +
                             "and (cons.constraint_type is null or cons.constraint_type = 'P')";

            try
            {
                var cmd = DbConnection.CreateCommand();
                cmd.CommandText = cmdText;
                cmd.CommandType = CommandType.Text;

                cmd.AddParameter(DbType.String, "tableName", tableName);

                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string columnName = reader.GetString(0);
                    string dataType   = reader.GetString(1);
                    result.AddParam(columnName, TypeAssoc[dataType]);
                    if (!reader.IsDBNull(2))
                    {
                        string constraint = reader.GetString(2);
                        if (constraint == "P")
                        {
                            result.PrimaryKeyName = columnName;
                        }
                    }
                }

                if (result.Count == 0)
                {
                    throw new TableMetadataRetrievalException(null, tableName);
                }
            }
            catch (Exception ex)
            {
                throw new TableMetadataRetrievalException(ex, tableName);
            }
            return(result);
        }