コード例 #1
0
        private void DataTableFromDataReaderDataTypes(DataTable dataTypesTable, OdbcDataReader dataReader, OdbcConnection connection) {

            DataTable       schemaTable = null;
            // 

            // Build a DataTable from the reader
            schemaTable = dataReader.GetSchemaTable();

            // vstfdevdiv:479715 Handle cases where reader is empty
            if (null == schemaTable) {
                throw ADP.OdbcNoTypesFromProvider();
            }
            
            object[] getTypeInfoValues = new object[schemaTable.Rows.Count];
            DataRow dataTypesRow;

            DataColumn typeNameColumn = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName];
            DataColumn providerDbTypeColumn = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn columnSizeColumn = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize];
            DataColumn createParametersColumn = dataTypesTable.Columns[DbMetaDataColumnNames.CreateParameters];
            DataColumn dataTypeColumn = dataTypesTable.Columns[DbMetaDataColumnNames.DataType];
            DataColumn isAutoIncermentableColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsAutoIncrementable];
            DataColumn isCaseSensitiveColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsCaseSensitive];
            DataColumn isFixedLengthColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn isFixedPrecisionScaleColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale];
            DataColumn isLongColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsLong];
            DataColumn isNullableColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable];
            DataColumn isSearchableColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn isSearchableWithLikeColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchableWithLike];
            DataColumn isUnsignedColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsUnsigned];
            DataColumn maximumScaleColumn = dataTypesTable.Columns[DbMetaDataColumnNames.MaximumScale];
            DataColumn minimumScaleColumn = dataTypesTable.Columns[DbMetaDataColumnNames.MinimumScale];
            DataColumn literalPrefixColumn = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralPrefix];
            DataColumn literalSuffixColumn = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralSuffix];
            DataColumn SQLTypeNameColumn = dataTypesTable.Columns[OdbcMetaDataColumnNames.SQLType];


            const int indexTYPE_NAME = 0;
            const int indexDATA_TYPE = 1;
            const int indexCOLUMN_SIZE = 2;
            const int indexCREATE_PARAMS = 5;
            const int indexAUTO_UNIQUE_VALUE = 11;
            const int indexCASE_SENSITIVE = 7;
            const int indexFIXED_PREC_SCALE = 10;
            const int indexNULLABLE = 6;
            const int indexSEARCHABLE = 8;
            const int indexUNSIGNED_ATTRIBUTE = 9;
            const int indexMAXIMUM_SCALE = 14;
            const int indexMINIMUM_SCALE = 13;
            const int indexLITERAL_PREFIX = 3;
            const int indexLITERAL_SUFFIX = 4;

            const int SQL_DATE_V2 = 9;
            const int SQL_TIME_V2 = 10;

            TypeMap typeMap ;


            while (dataReader.Read()) {
                dataReader.GetValues(getTypeInfoValues);
                dataTypesRow = dataTypesTable.NewRow();

                ODBC32.SQL_TYPE sqlType;

                dataTypesRow[typeNameColumn] = getTypeInfoValues[indexTYPE_NAME];
                dataTypesRow[SQLTypeNameColumn] = getTypeInfoValues[indexDATA_TYPE];

                sqlType = (ODBC32.SQL_TYPE)(Int32) Convert.ChangeType(getTypeInfoValues[indexDATA_TYPE],
                                                                      typeof(Int32), 
                                                                      (System.IFormatProvider)null);
                // if the driver is pre version 3 and it returned the v2 SQL_DATE or SQL_TIME types they need
                // to be mapped to thier v3 equlivants
                if (connection.IsV3Driver == false) {
                    if ((int)sqlType == SQL_DATE_V2) {
                        sqlType = ODBC32.SQL_TYPE.TYPE_DATE;
                    }
                    else if ((int)sqlType == SQL_TIME_V2) {
                        sqlType = ODBC32.SQL_TYPE.TYPE_TIME;
                    }
                }
                try {
                   typeMap = TypeMap.FromSqlType(sqlType);
                }
                // FromSqlType will throw an argument exception if it does not recognize the SqlType.
                // This is not an error since the GetTypeInfo DATA_TYPE may be a SQL data type or a driver specific
                // type. If there is no TypeMap for the type its not an error but it will degrade our level of
                // understanding of/ support for the type.
                catch (ArgumentException) {
                    typeMap = null;
                }

                // if we have a type map we can determine the dbType and the CLR type if not leave them null
                if (typeMap != null) {
                    dataTypesRow[providerDbTypeColumn] = typeMap._odbcType;
                    dataTypesRow[dataTypeColumn] = typeMap._type.FullName;
                    // setting isLong and isFixedLength only if we have a type map because for provider
                    // specific types we have no idea what the types attributes are if GetTypeInfo did not
                    // tell us
                    switch (sqlType) {

                        case ODBC32.SQL_TYPE.LONGVARCHAR:
                        case ODBC32.SQL_TYPE.WLONGVARCHAR:
                        case ODBC32.SQL_TYPE.LONGVARBINARY:
                        case ODBC32.SQL_TYPE.SS_XML:
                            dataTypesRow[isLongColumn] = true;
                            dataTypesRow[isFixedLengthColumn] = false;
                            break;

                        case ODBC32.SQL_TYPE.VARCHAR:
                        case ODBC32.SQL_TYPE.WVARCHAR:
                        case ODBC32.SQL_TYPE.VARBINARY:
                            dataTypesRow[isLongColumn] = false;
                            dataTypesRow[isFixedLengthColumn] = false;
                            break;

                        case ODBC32.SQL_TYPE.CHAR:
                        case ODBC32.SQL_TYPE.WCHAR:
                        case ODBC32.SQL_TYPE.DECIMAL:
                        case ODBC32.SQL_TYPE.NUMERIC:
                        case ODBC32.SQL_TYPE.SMALLINT:
                        case ODBC32.SQL_TYPE.INTEGER:
                        case ODBC32.SQL_TYPE.REAL:
                        case ODBC32.SQL_TYPE.FLOAT:
                        case ODBC32.SQL_TYPE.DOUBLE:
                        case ODBC32.SQL_TYPE.BIT:
                        case ODBC32.SQL_TYPE.TINYINT:
                        case ODBC32.SQL_TYPE.BIGINT:
                        case ODBC32.SQL_TYPE.TYPE_DATE:
                        case ODBC32.SQL_TYPE.TYPE_TIME:
                        case ODBC32.SQL_TYPE.TIMESTAMP:
                        case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                        case ODBC32.SQL_TYPE.GUID:
                        case ODBC32.SQL_TYPE.SS_VARIANT:
                        case ODBC32.SQL_TYPE.SS_UTCDATETIME:
                        case ODBC32.SQL_TYPE.SS_TIME_EX:
                        case ODBC32.SQL_TYPE.BINARY:    
                            dataTypesRow[isLongColumn] = false;
                            dataTypesRow[isFixedLengthColumn] = true;
                            break;

                        case ODBC32.SQL_TYPE.SS_UDT:
                        default:
                            // for User defined types don't know if its long or or if it is
                            // varaible length or not so leave the fields null
                            break;
                    }
                }



                dataTypesRow[columnSizeColumn] = getTypeInfoValues[indexCOLUMN_SIZE];
                dataTypesRow[createParametersColumn] = getTypeInfoValues[indexCREATE_PARAMS];

                if ((getTypeInfoValues[indexAUTO_UNIQUE_VALUE] == DBNull.Value) ||
                    (Convert.ToInt16(getTypeInfoValues[indexAUTO_UNIQUE_VALUE], null) == 0)) {
                    dataTypesRow[isAutoIncermentableColumn] = false;
                }
                else {
                    dataTypesRow[isAutoIncermentableColumn] = true;
                }

                dataTypesRow[isCaseSensitiveColumn] = BooleanFromODBC(getTypeInfoValues[indexCASE_SENSITIVE]);
                dataTypesRow[isFixedPrecisionScaleColumn] = BooleanFromODBC(getTypeInfoValues[indexFIXED_PREC_SCALE]);

                if (getTypeInfoValues[indexNULLABLE] != DBNull.Value) {
                    //Use Convert.ToInt16 instead of direct cast to short because the value will be Int32 in some cases
                    switch ((ODBC32.SQL_NULLABILITY)Convert.ToInt16(getTypeInfoValues[indexNULLABLE], null)) { 

                        case ODBC32.SQL_NULLABILITY.NO_NULLS:
                            dataTypesRow[isNullableColumn] = false;
                            break;

                        case ODBC32.SQL_NULLABILITY.NULLABLE:
                            dataTypesRow[isNullableColumn] = true;
                            break;

                        case ODBC32.SQL_NULLABILITY.UNKNOWN:
                            dataTypesRow[isNullableColumn] = DBNull.Value;
                            break;
                    }
                }

                if ( DBNull.Value != getTypeInfoValues[indexSEARCHABLE]){

                    //Use Convert.ToInt16 instead of direct cast to short because the value will be Int32 in some cases
                    Int16 searchableValue = Convert.ToInt16(getTypeInfoValues[indexSEARCHABLE], null);
                    switch (searchableValue){

                        case (Int16)ODBC32.SQL_SEARCHABLE.UNSEARCHABLE:
                            dataTypesRow[isSearchableColumn] = false;
                            dataTypesRow[isSearchableWithLikeColumn] = false;
                            break;

                        case (Int16)ODBC32.SQL_SEARCHABLE.LIKE_ONLY:
                             dataTypesRow[isSearchableColumn] = false;
                             dataTypesRow[isSearchableWithLikeColumn] = true;
                             break;

                        case (Int16)ODBC32.SQL_SEARCHABLE.ALL_EXCEPT_LIKE:
                            dataTypesRow[isSearchableColumn] = true;
                            dataTypesRow[isSearchableWithLikeColumn] = false;
                            break;

                        case (Int16)ODBC32.SQL_SEARCHABLE.SEARCHABLE:
                            dataTypesRow[isSearchableColumn] = true;
                            dataTypesRow[isSearchableWithLikeColumn] = true;
                            break;
                    }
                }

                dataTypesRow[isUnsignedColumn] = BooleanFromODBC(getTypeInfoValues[indexUNSIGNED_ATTRIBUTE]);

                //For assignment to the DataSet, don't cast the data types -- let the DataSet take care of any conversion
                if (getTypeInfoValues[indexMAXIMUM_SCALE] != DBNull.Value ) {
                    dataTypesRow[maximumScaleColumn] = getTypeInfoValues[indexMAXIMUM_SCALE];
                }

                if (getTypeInfoValues[indexMINIMUM_SCALE] != DBNull.Value ) {
                    dataTypesRow[minimumScaleColumn] = getTypeInfoValues[indexMINIMUM_SCALE];
                }

                if (getTypeInfoValues[indexLITERAL_PREFIX] != DBNull.Value ) {
                    dataTypesRow[literalPrefixColumn] = getTypeInfoValues[indexLITERAL_PREFIX];
                }

                if (getTypeInfoValues[indexLITERAL_SUFFIX] != DBNull.Value ) {
                    dataTypesRow[literalSuffixColumn] = getTypeInfoValues[indexLITERAL_SUFFIX];
                }

                dataTypesTable.Rows.Add(dataTypesRow);
            }

        }
        private void DataTableFromDataReaderDataTypes(DataTable dataTypesTable, OdbcDataReader dataReader, OdbcConnection connection)
        {
            DataTable schemaTable = null;
            schemaTable = dataReader.GetSchemaTable();
            if (schemaTable == null)
            {
                throw ADP.OdbcNoTypesFromProvider();
            }
            object[] values = new object[schemaTable.Rows.Count];
            DataColumn column19 = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName];
            DataColumn column18 = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn column17 = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize];
            DataColumn column16 = dataTypesTable.Columns[DbMetaDataColumnNames.CreateParameters];
            DataColumn column15 = dataTypesTable.Columns[DbMetaDataColumnNames.DataType];
            DataColumn column6 = dataTypesTable.Columns[DbMetaDataColumnNames.IsAutoIncrementable];
            DataColumn column14 = dataTypesTable.Columns[DbMetaDataColumnNames.IsCaseSensitive];
            DataColumn column5 = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn column13 = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale];
            DataColumn column4 = dataTypesTable.Columns[DbMetaDataColumnNames.IsLong];
            DataColumn column3 = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable];
            DataColumn column2 = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn column = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchableWithLike];
            DataColumn column12 = dataTypesTable.Columns[DbMetaDataColumnNames.IsUnsigned];
            DataColumn column11 = dataTypesTable.Columns[DbMetaDataColumnNames.MaximumScale];
            DataColumn column10 = dataTypesTable.Columns[DbMetaDataColumnNames.MinimumScale];
            DataColumn column9 = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralPrefix];
            DataColumn column8 = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralSuffix];
            DataColumn column7 = dataTypesTable.Columns[OdbcMetaDataColumnNames.SQLType];
            while (dataReader.Read())
            {
                TypeMap map;
                dataReader.GetValues(values);
                DataRow row = dataTypesTable.NewRow();
                row[column19] = values[0];
                row[column7] = values[1];
                ODBC32.SQL_TYPE sqltype = (ODBC32.SQL_TYPE) ((short) ((int) Convert.ChangeType(values[1], typeof(int), null)));
                if (!connection.IsV3Driver)
                {
                    if (sqltype == ~ODBC32.SQL_TYPE.WLONGVARCHAR)
                    {
                        sqltype = ODBC32.SQL_TYPE.TYPE_DATE;
                    }
                    else if (sqltype == ~ODBC32.SQL_TYPE.GUID)
                    {
                        sqltype = ODBC32.SQL_TYPE.TYPE_TIME;
                    }
                }
                try
                {
                    map = TypeMap.FromSqlType(sqltype);
                }
                catch (ArgumentException)
                {
                    map = null;
                }
                if (map != null)
                {
                    row[column18] = map._odbcType;
                    row[column15] = map._type.FullName;
                    switch (sqltype)
                    {
                        case ODBC32.SQL_TYPE.SS_TIME_EX:
                        case ODBC32.SQL_TYPE.SS_UTCDATETIME:
                        case ODBC32.SQL_TYPE.SS_VARIANT:
                        case ODBC32.SQL_TYPE.GUID:
                        case ODBC32.SQL_TYPE.WCHAR:
                        case ODBC32.SQL_TYPE.BIT:
                        case ODBC32.SQL_TYPE.TINYINT:
                        case ODBC32.SQL_TYPE.BIGINT:
                        case ODBC32.SQL_TYPE.BINARY:
                        case ODBC32.SQL_TYPE.CHAR:
                        case ODBC32.SQL_TYPE.NUMERIC:
                        case ODBC32.SQL_TYPE.DECIMAL:
                        case ODBC32.SQL_TYPE.INTEGER:
                        case ODBC32.SQL_TYPE.SMALLINT:
                        case ODBC32.SQL_TYPE.FLOAT:
                        case ODBC32.SQL_TYPE.REAL:
                        case ODBC32.SQL_TYPE.DOUBLE:
                        case ODBC32.SQL_TYPE.TIMESTAMP:
                        case ODBC32.SQL_TYPE.TYPE_DATE:
                        case ODBC32.SQL_TYPE.TYPE_TIME:
                        case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                            goto Label_02F8;

                        case ODBC32.SQL_TYPE.SS_XML:
                        case ODBC32.SQL_TYPE.WLONGVARCHAR:
                        case ODBC32.SQL_TYPE.LONGVARBINARY:
                        case ODBC32.SQL_TYPE.LONGVARCHAR:
                            goto Label_02BC;

                        case ODBC32.SQL_TYPE.WVARCHAR:
                        case ODBC32.SQL_TYPE.VARBINARY:
                        case ODBC32.SQL_TYPE.VARCHAR:
                            goto Label_02DA;
                    }
                }
                goto Label_0314;
            Label_02BC:
                row[column4] = true;
                row[column5] = false;
                goto Label_0314;
            Label_02DA:
                row[column4] = false;
                row[column5] = false;
                goto Label_0314;
            Label_02F8:
                row[column4] = false;
                row[column5] = true;
            Label_0314:
                row[column17] = values[2];
                row[column16] = values[5];
                if ((values[11] == DBNull.Value) || (Convert.ToInt16(values[11], null) == 0))
                {
                    row[column6] = false;
                }
                else
                {
                    row[column6] = true;
                }
                row[column14] = this.BooleanFromODBC(values[7]);
                row[column13] = this.BooleanFromODBC(values[10]);
                if (values[6] != DBNull.Value)
                {
                    switch (((ODBC32.SQL_NULLABILITY) ((ushort) Convert.ToInt16(values[6], null))))
                    {
                        case ODBC32.SQL_NULLABILITY.NO_NULLS:
                            row[column3] = false;
                            break;

                        case ODBC32.SQL_NULLABILITY.NULLABLE:
                            row[column3] = true;
                            break;

                        case ODBC32.SQL_NULLABILITY.UNKNOWN:
                            row[column3] = DBNull.Value;
                            break;
                    }
                }
                if (DBNull.Value != values[8])
                {
                    switch (Convert.ToInt16(values[8], null))
                    {
                        case 0:
                            row[column2] = false;
                            row[column] = false;
                            break;

                        case 1:
                            row[column2] = false;
                            row[column] = true;
                            break;

                        case 2:
                            row[column2] = true;
                            row[column] = false;
                            break;

                        case 3:
                            row[column2] = true;
                            row[column] = true;
                            break;
                    }
                }
                row[column12] = this.BooleanFromODBC(values[9]);
                if (values[14] != DBNull.Value)
                {
                    row[column11] = values[14];
                }
                if (values[13] != DBNull.Value)
                {
                    row[column10] = values[13];
                }
                if (values[3] != DBNull.Value)
                {
                    row[column9] = values[3];
                }
                if (values[4] != DBNull.Value)
                {
                    row[column8] = values[4];
                }
                dataTypesTable.Rows.Add(row);
            }
        }
コード例 #3
0
        private static void DataTableFromDataReaderDataTypes(DataTable dataTypesTable, OdbcDataReader dataReader, OdbcConnection connection)
        {
            DataTable?schemaTable;

            //

            // Build a DataTable from the reader
            schemaTable = dataReader.GetSchemaTable();

            // vstfdevdiv:479715 Handle cases where reader is empty
            if (null == schemaTable)
            {
                throw ADP.OdbcNoTypesFromProvider();
            }

            object[] getTypeInfoValues = new object[schemaTable.Rows.Count];
            DataRow  dataTypesRow;

            DataColumn typeNameColumn              = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName] !;
            DataColumn providerDbTypeColumn        = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType] !;
            DataColumn columnSizeColumn            = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize] !;
            DataColumn createParametersColumn      = dataTypesTable.Columns[DbMetaDataColumnNames.CreateParameters] !;
            DataColumn dataTypeColumn              = dataTypesTable.Columns[DbMetaDataColumnNames.DataType] !;
            DataColumn isAutoIncermentableColumn   = dataTypesTable.Columns[DbMetaDataColumnNames.IsAutoIncrementable] !;
            DataColumn isCaseSensitiveColumn       = dataTypesTable.Columns[DbMetaDataColumnNames.IsCaseSensitive] !;
            DataColumn isFixedLengthColumn         = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength] !;
            DataColumn isFixedPrecisionScaleColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale] !;
            DataColumn isLongColumn               = dataTypesTable.Columns[DbMetaDataColumnNames.IsLong] !;
            DataColumn isNullableColumn           = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable] !;
            DataColumn isSearchableColumn         = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable] !;
            DataColumn isSearchableWithLikeColumn = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchableWithLike] !;
            DataColumn isUnsignedColumn           = dataTypesTable.Columns[DbMetaDataColumnNames.IsUnsigned] !;
            DataColumn maximumScaleColumn         = dataTypesTable.Columns[DbMetaDataColumnNames.MaximumScale] !;
            DataColumn minimumScaleColumn         = dataTypesTable.Columns[DbMetaDataColumnNames.MinimumScale] !;
            DataColumn literalPrefixColumn        = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralPrefix] !;
            DataColumn literalSuffixColumn        = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralSuffix] !;
            DataColumn SQLTypeNameColumn          = dataTypesTable.Columns[OdbcMetaDataColumnNames.SQLType] !;


            const int indexTYPE_NAME          = 0;
            const int indexDATA_TYPE          = 1;
            const int indexCOLUMN_SIZE        = 2;
            const int indexCREATE_PARAMS      = 5;
            const int indexAUTO_UNIQUE_VALUE  = 11;
            const int indexCASE_SENSITIVE     = 7;
            const int indexFIXED_PREC_SCALE   = 10;
            const int indexNULLABLE           = 6;
            const int indexSEARCHABLE         = 8;
            const int indexUNSIGNED_ATTRIBUTE = 9;
            const int indexMAXIMUM_SCALE      = 14;
            const int indexMINIMUM_SCALE      = 13;
            const int indexLITERAL_PREFIX     = 3;
            const int indexLITERAL_SUFFIX     = 4;

            const int SQL_DATE_V2 = 9;
            const int SQL_TIME_V2 = 10;

            TypeMap?typeMap;


            while (dataReader.Read())
            {
                dataReader.GetValues(getTypeInfoValues);
                dataTypesRow = dataTypesTable.NewRow();

                ODBC32.SQL_TYPE sqlType;

                dataTypesRow[typeNameColumn]    = getTypeInfoValues[indexTYPE_NAME];
                dataTypesRow[SQLTypeNameColumn] = getTypeInfoValues[indexDATA_TYPE];

                sqlType = (ODBC32.SQL_TYPE)(int) Convert.ChangeType(getTypeInfoValues[indexDATA_TYPE], typeof(int), null);
                // if the driver is pre version 3 and it returned the v2 SQL_DATE or SQL_TIME types they need
                // to be mapped to their v3 equivalent
                if (connection.IsV3Driver == false)
                {
                    if ((int)sqlType == SQL_DATE_V2)
                    {
                        sqlType = ODBC32.SQL_TYPE.TYPE_DATE;
                    }
                    else if ((int)sqlType == SQL_TIME_V2)
                    {
                        sqlType = ODBC32.SQL_TYPE.TYPE_TIME;
                    }
                }
                try
                {
                    typeMap = TypeMap.FromSqlType(sqlType);
                }
                // FromSqlType will throw an argument exception if it does not recognize the SqlType.
                // This is not an error since the GetTypeInfo DATA_TYPE may be a SQL data type or a driver specific
                // type. If there is no TypeMap for the type its not an error but it will degrade our level of
                // understanding of/ support for the type.
                catch (ArgumentException)
                {
                    typeMap = null;
                }

                // if we have a type map we can determine the dbType and the CLR type if not leave them null
                if (typeMap != null)
                {
                    dataTypesRow[providerDbTypeColumn] = typeMap._odbcType;
                    dataTypesRow[dataTypeColumn]       = typeMap._type.FullName;
                    // setting isLong and isFixedLength only if we have a type map because for provider
                    // specific types we have no idea what the types attributes are if GetTypeInfo did not
                    // tell us
                    switch (sqlType)
                    {
                    case ODBC32.SQL_TYPE.LONGVARCHAR:
                    case ODBC32.SQL_TYPE.WLONGVARCHAR:
                    case ODBC32.SQL_TYPE.LONGVARBINARY:
                    case ODBC32.SQL_TYPE.SS_XML:
                        dataTypesRow[isLongColumn]        = true;
                        dataTypesRow[isFixedLengthColumn] = false;
                        break;

                    case ODBC32.SQL_TYPE.VARCHAR:
                    case ODBC32.SQL_TYPE.WVARCHAR:
                    case ODBC32.SQL_TYPE.VARBINARY:
                        dataTypesRow[isLongColumn]        = false;
                        dataTypesRow[isFixedLengthColumn] = false;
                        break;

                    case ODBC32.SQL_TYPE.CHAR:
                    case ODBC32.SQL_TYPE.WCHAR:
                    case ODBC32.SQL_TYPE.DECIMAL:
                    case ODBC32.SQL_TYPE.NUMERIC:
                    case ODBC32.SQL_TYPE.SMALLINT:
                    case ODBC32.SQL_TYPE.INTEGER:
                    case ODBC32.SQL_TYPE.REAL:
                    case ODBC32.SQL_TYPE.FLOAT:
                    case ODBC32.SQL_TYPE.DOUBLE:
                    case ODBC32.SQL_TYPE.BIT:
                    case ODBC32.SQL_TYPE.TINYINT:
                    case ODBC32.SQL_TYPE.BIGINT:
                    case ODBC32.SQL_TYPE.TYPE_DATE:
                    case ODBC32.SQL_TYPE.TYPE_TIME:
                    case ODBC32.SQL_TYPE.TIMESTAMP:
                    case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                    case ODBC32.SQL_TYPE.GUID:
                    case ODBC32.SQL_TYPE.SS_VARIANT:
                    case ODBC32.SQL_TYPE.SS_UTCDATETIME:
                    case ODBC32.SQL_TYPE.SS_TIME_EX:
                    case ODBC32.SQL_TYPE.BINARY:
                        dataTypesRow[isLongColumn]        = false;
                        dataTypesRow[isFixedLengthColumn] = true;
                        break;

                    case ODBC32.SQL_TYPE.SS_UDT:
                    default:
                        // for User defined types don't know if its long or if it is
                        // variable length or not so leave the fields null
                        break;
                    }
                }



                dataTypesRow[columnSizeColumn]       = getTypeInfoValues[indexCOLUMN_SIZE];
                dataTypesRow[createParametersColumn] = getTypeInfoValues[indexCREATE_PARAMS];

                if ((getTypeInfoValues[indexAUTO_UNIQUE_VALUE] == DBNull.Value) ||
                    (Convert.ToInt16(getTypeInfoValues[indexAUTO_UNIQUE_VALUE], null) == 0))
                {
                    dataTypesRow[isAutoIncermentableColumn] = false;
                }
                else
                {
                    dataTypesRow[isAutoIncermentableColumn] = true;
                }

                dataTypesRow[isCaseSensitiveColumn]       = BooleanFromODBC(getTypeInfoValues[indexCASE_SENSITIVE]);
                dataTypesRow[isFixedPrecisionScaleColumn] = BooleanFromODBC(getTypeInfoValues[indexFIXED_PREC_SCALE]);

                if (getTypeInfoValues[indexNULLABLE] != DBNull.Value)
                {
                    //Use Convert.ToInt16 instead of direct cast to short because the value will be Int32 in some cases
                    switch ((ODBC32.SQL_NULLABILITY)Convert.ToInt16(getTypeInfoValues[indexNULLABLE], null))
                    {
                    case ODBC32.SQL_NULLABILITY.NO_NULLS:
                        dataTypesRow[isNullableColumn] = false;
                        break;

                    case ODBC32.SQL_NULLABILITY.NULLABLE:
                        dataTypesRow[isNullableColumn] = true;
                        break;

                    case ODBC32.SQL_NULLABILITY.UNKNOWN:
                        dataTypesRow[isNullableColumn] = DBNull.Value;
                        break;
                    }
                }

                if (DBNull.Value != getTypeInfoValues[indexSEARCHABLE])
                {
                    //Use Convert.ToInt16 instead of direct cast to short because the value will be Int32 in some cases
                    short searchableValue = Convert.ToInt16(getTypeInfoValues[indexSEARCHABLE], null);
                    switch (searchableValue)
                    {
                    case (short)ODBC32.SQL_SEARCHABLE.UNSEARCHABLE:
                        dataTypesRow[isSearchableColumn]         = false;
                        dataTypesRow[isSearchableWithLikeColumn] = false;
                        break;

                    case (short)ODBC32.SQL_SEARCHABLE.LIKE_ONLY:
                        dataTypesRow[isSearchableColumn]         = false;
                        dataTypesRow[isSearchableWithLikeColumn] = true;
                        break;

                    case (short)ODBC32.SQL_SEARCHABLE.ALL_EXCEPT_LIKE:
                        dataTypesRow[isSearchableColumn]         = true;
                        dataTypesRow[isSearchableWithLikeColumn] = false;
                        break;

                    case (short)ODBC32.SQL_SEARCHABLE.SEARCHABLE:
                        dataTypesRow[isSearchableColumn]         = true;
                        dataTypesRow[isSearchableWithLikeColumn] = true;
                        break;
                    }
                }

                dataTypesRow[isUnsignedColumn] = BooleanFromODBC(getTypeInfoValues[indexUNSIGNED_ATTRIBUTE]);

                //For assignment to the DataSet, don't cast the data types -- let the DataSet take care of any conversion
                if (getTypeInfoValues[indexMAXIMUM_SCALE] != DBNull.Value)
                {
                    dataTypesRow[maximumScaleColumn] = getTypeInfoValues[indexMAXIMUM_SCALE];
                }

                if (getTypeInfoValues[indexMINIMUM_SCALE] != DBNull.Value)
                {
                    dataTypesRow[minimumScaleColumn] = getTypeInfoValues[indexMINIMUM_SCALE];
                }

                if (getTypeInfoValues[indexLITERAL_PREFIX] != DBNull.Value)
                {
                    dataTypesRow[literalPrefixColumn] = getTypeInfoValues[indexLITERAL_PREFIX];
                }

                if (getTypeInfoValues[indexLITERAL_SUFFIX] != DBNull.Value)
                {
                    dataTypesRow[literalSuffixColumn] = getTypeInfoValues[indexLITERAL_SUFFIX];
                }

                dataTypesTable.Rows.Add(dataTypesRow);
            }
        }
コード例 #4
0
        private void DataTableFromDataReaderDataTypes(DataTable dataTypesTable, OdbcDataReader dataReader, OdbcConnection connection)
        {
            DataTable schemaTable = null;

            schemaTable = dataReader.GetSchemaTable();
            if (schemaTable == null)
            {
                throw ADP.OdbcNoTypesFromProvider();
            }
            object[]   values   = new object[schemaTable.Rows.Count];
            DataColumn column19 = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName];
            DataColumn column18 = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn column17 = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize];
            DataColumn column16 = dataTypesTable.Columns[DbMetaDataColumnNames.CreateParameters];
            DataColumn column15 = dataTypesTable.Columns[DbMetaDataColumnNames.DataType];
            DataColumn column6  = dataTypesTable.Columns[DbMetaDataColumnNames.IsAutoIncrementable];
            DataColumn column14 = dataTypesTable.Columns[DbMetaDataColumnNames.IsCaseSensitive];
            DataColumn column5  = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn column13 = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedPrecisionScale];
            DataColumn column4  = dataTypesTable.Columns[DbMetaDataColumnNames.IsLong];
            DataColumn column3  = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable];
            DataColumn column2  = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn column   = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchableWithLike];
            DataColumn column12 = dataTypesTable.Columns[DbMetaDataColumnNames.IsUnsigned];
            DataColumn column11 = dataTypesTable.Columns[DbMetaDataColumnNames.MaximumScale];
            DataColumn column10 = dataTypesTable.Columns[DbMetaDataColumnNames.MinimumScale];
            DataColumn column9  = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralPrefix];
            DataColumn column8  = dataTypesTable.Columns[DbMetaDataColumnNames.LiteralSuffix];
            DataColumn column7  = dataTypesTable.Columns[OdbcMetaDataColumnNames.SQLType];

            while (dataReader.Read())
            {
                TypeMap map;
                dataReader.GetValues(values);
                DataRow row = dataTypesTable.NewRow();
                row[column19] = values[0];
                row[column7]  = values[1];
                ODBC32.SQL_TYPE sqltype = (ODBC32.SQL_TYPE)((short)((int)Convert.ChangeType(values[1], typeof(int), null)));
                if (!connection.IsV3Driver)
                {
                    if (sqltype == ~ODBC32.SQL_TYPE.WLONGVARCHAR)
                    {
                        sqltype = ODBC32.SQL_TYPE.TYPE_DATE;
                    }
                    else if (sqltype == ~ODBC32.SQL_TYPE.GUID)
                    {
                        sqltype = ODBC32.SQL_TYPE.TYPE_TIME;
                    }
                }
                try
                {
                    map = TypeMap.FromSqlType(sqltype);
                }
                catch (ArgumentException)
                {
                    map = null;
                }
                if (map != null)
                {
                    row[column18] = map._odbcType;
                    row[column15] = map._type.FullName;
                    switch (sqltype)
                    {
                    case ODBC32.SQL_TYPE.SS_TIME_EX:
                    case ODBC32.SQL_TYPE.SS_UTCDATETIME:
                    case ODBC32.SQL_TYPE.SS_VARIANT:
                    case ODBC32.SQL_TYPE.GUID:
                    case ODBC32.SQL_TYPE.WCHAR:
                    case ODBC32.SQL_TYPE.BIT:
                    case ODBC32.SQL_TYPE.TINYINT:
                    case ODBC32.SQL_TYPE.BIGINT:
                    case ODBC32.SQL_TYPE.BINARY:
                    case ODBC32.SQL_TYPE.CHAR:
                    case ODBC32.SQL_TYPE.NUMERIC:
                    case ODBC32.SQL_TYPE.DECIMAL:
                    case ODBC32.SQL_TYPE.INTEGER:
                    case ODBC32.SQL_TYPE.SMALLINT:
                    case ODBC32.SQL_TYPE.FLOAT:
                    case ODBC32.SQL_TYPE.REAL:
                    case ODBC32.SQL_TYPE.DOUBLE:
                    case ODBC32.SQL_TYPE.TIMESTAMP:
                    case ODBC32.SQL_TYPE.TYPE_DATE:
                    case ODBC32.SQL_TYPE.TYPE_TIME:
                    case ODBC32.SQL_TYPE.TYPE_TIMESTAMP:
                        goto Label_02F8;

                    case ODBC32.SQL_TYPE.SS_XML:
                    case ODBC32.SQL_TYPE.WLONGVARCHAR:
                    case ODBC32.SQL_TYPE.LONGVARBINARY:
                    case ODBC32.SQL_TYPE.LONGVARCHAR:
                        goto Label_02BC;

                    case ODBC32.SQL_TYPE.WVARCHAR:
                    case ODBC32.SQL_TYPE.VARBINARY:
                    case ODBC32.SQL_TYPE.VARCHAR:
                        goto Label_02DA;
                    }
                }
                goto Label_0314;
Label_02BC:
                row[column4] = true;
                row[column5] = false;
                goto Label_0314;
Label_02DA:
                row[column4] = false;
                row[column5] = false;
                goto Label_0314;
Label_02F8:
                row[column4] = false;
                row[column5] = true;
Label_0314:
                row[column17] = values[2];
                row[column16] = values[5];
                if ((values[11] == DBNull.Value) || (Convert.ToInt16(values[11], null) == 0))
                {
                    row[column6] = false;
                }
                else
                {
                    row[column6] = true;
                }
                row[column14] = this.BooleanFromODBC(values[7]);
                row[column13] = this.BooleanFromODBC(values[10]);
                if (values[6] != DBNull.Value)
                {
                    switch (((ODBC32.SQL_NULLABILITY)((ushort)Convert.ToInt16(values[6], null))))
                    {
                    case ODBC32.SQL_NULLABILITY.NO_NULLS:
                        row[column3] = false;
                        break;

                    case ODBC32.SQL_NULLABILITY.NULLABLE:
                        row[column3] = true;
                        break;

                    case ODBC32.SQL_NULLABILITY.UNKNOWN:
                        row[column3] = DBNull.Value;
                        break;
                    }
                }
                if (DBNull.Value != values[8])
                {
                    switch (Convert.ToInt16(values[8], null))
                    {
                    case 0:
                        row[column2] = false;
                        row[column]  = false;
                        break;

                    case 1:
                        row[column2] = false;
                        row[column]  = true;
                        break;

                    case 2:
                        row[column2] = true;
                        row[column]  = false;
                        break;

                    case 3:
                        row[column2] = true;
                        row[column]  = true;
                        break;
                    }
                }
                row[column12] = this.BooleanFromODBC(values[9]);
                if (values[14] != DBNull.Value)
                {
                    row[column11] = values[14];
                }
                if (values[13] != DBNull.Value)
                {
                    row[column10] = values[13];
                }
                if (values[3] != DBNull.Value)
                {
                    row[column9] = values[3];
                }
                if (values[4] != DBNull.Value)
                {
                    row[column8] = values[4];
                }
                dataTypesTable.Rows.Add(row);
            }
        }