Пример #1
0
        internal static ColumnInfo FromSqlSchemaRow(DataRow row)
        {
            ColumnInfo ci = new ColumnInfo();

            ci.columnName = (string)row["column_name"];

            object o = row["ordinal_position"];

            if (o is short)
            {
                ci.ordinalPosition = (short)o;
            }
            else
            {
                ci.ordinalPosition = (int)o;
            }
            ci.dataType = DbTypeUtility.GetDbTypeFromSqlTypeName((string)row["data_type"]);
            string[] type = ((string)row["data_type"]).Split(' ');
            if (type.Length > 1 && type[1].ToLower() == "identity")
            {
                ci.isIdentity = true;
            }
            ci.nullable = ((string)row["is_nullable"] == "YES");
            //Length
            o = row["character_maximum_length"];
            if (o is DBNull)
            {
                ci.length = 0;
            }
            else if (o is short)
            {
                ci.length = (short)o;
            }
            else if (o is int)
            {
                ci.length = (int)o;
            }
            o = row["numeric_precision"];
            if (o is DBNull)
            {
                ci.precition = 0;
            }
            else
            {
                ci.precition = (byte)o;
            }
            o = row["numeric_scale"];
            if (o is DBNull)
            {
                ci.scale = 0;
            }
            else
            {
                ci.scale = (int)o;
            }
            return(ci);
        }
Пример #2
0
        internal static ColumnInfo FromOracleSchemaRow(DataRow row)
        {
            ColumnInfo ci = new ColumnInfo();

            ci.columnName      = (string)row["COLUMN_NAME"];
            ci.ordinalPosition = (int)(decimal)row["ID"];
            ci.dataType        = DbTypeUtility.GetDbTypeFromOracleTypeName((string)row["DATATYPE"]);
            ci.nullable        = ((string)row["NULLABLE"] != "N");
            //Length
            object o = row["LENGTH"];

            if (o is DBNull)
            {
                ci.length = 0;
            }
            else if (o is decimal)
            {
                ci.length = (int)(decimal)o;
            }
            else if (o is int)
            {
                ci.length = (int)o;
            }
            if (((string)row["DATATYPE"]).ToLower() == "long")
            {
                ci.length = int.MaxValue;
            }
            o = row["PRECISION"];
            if (o is DBNull)
            {
                ci.precition = 0;
            }
            else if (o is decimal)
            {
                ci.precition = (byte)(decimal)o;
            }
            else if (o is byte)
            {
                ci.precition = (byte)o;
            }
            o = row["SCALE"];
            if (o is DBNull)
            {
                ci.scale = 0;
            }
            else if (o is decimal)
            {
                ci.scale = (int)(decimal)o;
            }
            else
            {
                ci.scale = (int)o;
            }
            return(ci);
        }
Пример #3
0
        public static ColumnInfo FromSqlSpColumnsRow(DataRow row)
        {
            ColumnInfo ci = new ColumnInfo();

            ci.columnName = (string)row["COLUMN_NAME"];

            object o = row["ORDINAL_POSITION"];

            if (o is short)
            {
                ci.ordinalPosition = (short)o;
            }
            else
            {
                ci.ordinalPosition = (int)o;
            }
            ci.dataType = DbTypeUtility.GetDbTypeFromSqlTypeName((string)row["TYPE_NAME"]);
            string[] type = ((string)row["TYPE_NAME"]).Split(' ');
            if (type.Length > 1 && type[1].ToLower() == "identity")
            {
                ci.isIdentity = true;
            }
            ci.nullable = ((short)row["NULLABLE"] == 1);
            //Length
            o = row["CHAR_OCTET_LENGTH"];
            if (o is DBNull)
            {
                ci.length = 0;
            }
            else if (o is short)
            {
                ci.length = (short)o;
            }
            else if (o is int)
            {
                ci.length = (int)o;
            }
            if (ci.dataType == DbType.String || ci.dataType == DbType.StringFixedLength)
            {
                ci.length /= 2;
            }
            if (ci.dataType == DbType.Decimal || ci.dataType == DbType.Currency)
            {
                o = row["PRECISION"];
                if (o is DBNull)
                {
                    ci.precition = 0;
                }
                else
                {
                    ci.precition = (byte)(int)o;
                }
                o = row["SCALE"];
                if (o is DBNull)
                {
                    ci.scale = 0;
                }
                else
                {
                    ci.scale = (short)o;
                }
            }
            return(ci);
        }
Пример #4
0
        public string GetColumnDefinition(DatabaseType databaseType, bool isAlter = false)
        {
            string type = "";

            switch (databaseType)
            {
            case DatabaseType.SQLServer:
                type += DbTypeUtility.GetSqlTypeNameFromDbType(dataType, TypeName);
                break;

            case DatabaseType.Oracle:
                type += DbTypeUtility.GetOracleTypeNameFromDbType(dataType);
                break;

            case DatabaseType.OleDb:
                type += DbTypeUtility.GetOleDbTypeNameFromDbType(dataType);
                break;
            }
            switch (dataType)
            {
            case DbType.AnsiString:
                if (length > 10000)
                {
                    type = "varchar(MAX)";
                }
                else
                {
                    goto case DbType.AnsiStringFixedLength;
                }
                break;

            case DbType.String:
                if (length > 10000)
                {
                    type = "nvarchar(MAX)";
                }
                else
                {
                    goto case DbType.AnsiStringFixedLength;
                }
                break;

            case DbType.AnsiStringFixedLength:
            case DbType.StringFixedLength:
                type += String.Format("({0})", length);
                break;

            case DbType.Decimal:
                type += String.Format("({0},{1})", precition, scale);
                break;
            }
            string def;

            if (databaseType == DatabaseType.SQLServer)
            {
                def = String.Format("[{0}] {1}", columnName, type);
            }
            else
            {
                def = columnName + " " + type;
            }
            //Nullable
            def += (this.nullable ? " NULL" : " NOT NULL");
            //Default value
            if (DefaultValue != null && !isAlter)
            {
                def += " DEFAULT(" + DefaultValue.ToString() + ")";
            }
            //Identity
            if (this.isIdentity)
            {
                def += " IDENTITY";
            }
            //Primary key
            if (this.isPrimaryKey)
            {
                def += " PRIMARY KEY";
            }
            return(def);
        }