SqlDataRecord.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Data / Microsoft / SqlServer / Server / SqlDataRecord.cs / 1305376 / SqlDataRecord.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// [....] 
// [....]
// [....] 
// [....] 
//-----------------------------------------------------------------------------
 
namespace Microsoft.SqlServer.Server {

    using System;
    using System.Data; 
    using System.Data.Common;
    using System.Data.ProviderBase; 
    using System.Data.Sql; 
    using System.Data.SqlTypes;
    using System.Data.SqlClient; 
    using System.Diagnostics;

    public class SqlDataRecord : IDataRecord {
        SmiRecordBuffer         _recordBuffer; 
        SmiContext              _recordContext;
        SmiExtendedMetaData[]   _columnSmiMetaData; 
        SmiEventSink_Default    _eventSink; 
        SqlMetaData[]           _columnMetaData;
        FieldNameLookup         _fieldNameLookup; 
        bool                    _usesStringStorageForXml;

        static readonly SmiMetaData __maxNVarCharForXml = new SmiMetaData(SqlDbType.NVarChar, SmiMetaData.UnlimitedMaxLengthIndicator,
                                        SmiMetaData.DefaultNVarChar_NoCollation.Precision, 
                                        SmiMetaData.DefaultNVarChar_NoCollation.Scale,
                                        SmiMetaData.DefaultNVarChar.LocaleId, 
                                        SmiMetaData.DefaultNVarChar.CompareOptions, 
                                        null);
 
        public virtual int FieldCount {
            get {
                EnsureSubclassOverride();
                return _columnMetaData.Length; 
            }
        } 
 
        public virtual String GetName( int ordinal ) {
            EnsureSubclassOverride(); 
            return GetSqlMetaData(ordinal).Name;
        }

        public virtual String GetDataTypeName( int ordinal ) { 
            EnsureSubclassOverride();
            SqlMetaData metaData = GetSqlMetaData(ordinal); 
            if ( SqlDbType.Udt == metaData.SqlDbType ) { 
                return metaData.UdtTypeName;
            } 
            else {
                return MetaType.GetMetaTypeFromSqlDbType(metaData.SqlDbType, false).TypeName;
            }
        } 

        public virtual Type GetFieldType( int ordinal ) { 
            EnsureSubclassOverride(); 
            if (SqlDbType.Udt == GetSqlMetaData(ordinal).SqlDbType) {
                return GetSqlMetaData( ordinal ).Type; 
            }
            else {
                SqlMetaData md = GetSqlMetaData(ordinal);
                return MetaType.GetMetaTypeFromSqlDbType(md.SqlDbType, false).ClassType; 
            }
        } 
 
        public virtual Object GetValue(int ordinal) {
            EnsureSubclassOverride(); 
            SmiMetaData metaData = GetSmiMetaData(ordinal);

            if (SmiVersion >= SmiContextFactory.KatmaiVersion) {
                return ValueUtilsSmi.GetValue200( 
                                _eventSink,
                                _recordBuffer, 
                                ordinal, 
                                metaData,
                                _recordContext 
                                );
            }
            else {
                return ValueUtilsSmi.GetValue( 
                                _eventSink,
                                (ITypedGettersV3)_recordBuffer, 
                                ordinal, 
                                metaData,
                                _recordContext 
                                );
            }
        }
 
        public virtual int GetValues( object[] values ) {
            EnsureSubclassOverride(); 
            if (null == values) { 
                throw ADP.ArgumentNull("values");
            } 

            int copyLength = ( values.Length < FieldCount ) ? values.Length : FieldCount;
            for(int i=0; i= SmiContextFactory.KatmaiVersion) {
                return ValueUtilsSmi.GetSqlValue200(_eventSink, _recordBuffer, ordinal, metaData, _recordContext); 
            }
            return ValueUtilsSmi.GetSqlValue( _eventSink, _recordBuffer, ordinal, metaData, _recordContext );
        }
 
        public virtual int GetSqlValues(object[] values) {
            EnsureSubclassOverride(); 
            if (null == values) { 
                throw ADP.ArgumentNull("values");
            } 


            int copyLength = (values.Length < FieldCount) ? values.Length : FieldCount;
            for(int i=0; i FieldCount ) ? FieldCount : values.Length; 

            ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode [copyLength]; 

            // Verify all data values as acceptable before changing current state.
            for (int i=0; i < copyLength; i++) {
                SqlMetaData metaData = GetSqlMetaData(i); 
                typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
                    metaData.SqlDbType, false /* isMultiValued */, values[i], metaData.Type, SmiVersion); 
                if ( ExtendedClrTypeCode.Invalid == typeCodes[i] ) { 
                    throw ADP.InvalidCast();
                } 
            }

            // Now move the data (it'll only throw if someone plays with the values array between
            //      the validation loop and here, or if an invalid UDT was sent). 
            for( int i=0; i < copyLength; i++ ) {
                if (SmiVersion >= SmiContextFactory.KatmaiVersion) { 
                    ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0, 0, null); 
                }
                else { 
                    ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0);
                }
            }
 
            return copyLength;
        } 
 
        public virtual void SetValue( int ordinal, object value ) {
            EnsureSubclassOverride(); 
            SqlMetaData metaData = GetSqlMetaData(ordinal);
            ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
                        metaData.SqlDbType, false /* isMultiValued */, value, metaData.Type, SmiVersion);
            if ( ExtendedClrTypeCode.Invalid == typeCode ) { 
                    throw ADP.InvalidCast();
            } 
 
            if (SmiVersion >= SmiContextFactory.KatmaiVersion) {
                ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, 0, 0, null); 
            }
            else {
                ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, 0);
            } 
        }
 
        public virtual void SetBoolean(int ordinal, bool value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetBoolean(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetByte(int ordinal, byte value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetByte(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetBytes(int ordinal, long fieldOffset, byte[] buffer, int bufferOffset, int length) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetBytes(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), fieldOffset, buffer, bufferOffset, length);
        }

        public virtual void SetChar(int ordinal,  char value) { 
            EnsureSubclassOverride();
            throw ADP.NotSupported(); 
        } 

        public virtual void SetChars(int ordinal, long fieldOffset, char[] buffer, int bufferOffset, int length) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetChars(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), fieldOffset, buffer, bufferOffset, length);
        }
 
        public virtual void SetInt16(int ordinal,  System.Int16 value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetInt16(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetInt32(int ordinal, System.Int32 value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetInt32(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetInt64(int ordinal, System.Int64 value) { 
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetInt64(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetFloat(int ordinal, float value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSingle(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetDouble(int ordinal,  double value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetDouble(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetString(int ordinal, string value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetString(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetDecimal(int ordinal,  Decimal value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetDecimal(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }

        public virtual void SetDateTime(int ordinal,  DateTime value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetDateTime(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        } 

        public virtual void SetTimeSpan(int ordinal, TimeSpan value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetTimeSpan(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, SmiVersion >= SmiContextFactory.KatmaiVersion);
        }
 
        public virtual void SetDateTimeOffset(int ordinal, DateTimeOffset value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetDateTimeOffset(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, SmiVersion >= SmiContextFactory.KatmaiVersion); 
        }
 
        public virtual void SetDBNull(int ordinal) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetDBNull(_eventSink, _recordBuffer, ordinal, true);
        } 

        public virtual void SetGuid(int ordinal, Guid value) { 
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetGuid(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetSqlBoolean(int ordinal, SqlBoolean value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlBoolean(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetSqlByte(int ordinal, SqlByte value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlByte(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetSqlInt16(int ordinal, SqlInt16 value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlInt16(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetSqlInt32(int ordinal, SqlInt32 value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlInt32(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }

        public virtual void SetSqlInt64(int ordinal, SqlInt64 value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlInt64(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        } 

        public virtual void SetSqlSingle(int ordinal, SqlSingle value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlSingle(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }
 
        public virtual void SetSqlDouble(int ordinal, SqlDouble value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlDouble(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetSqlMoney(int ordinal, SqlMoney value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlMoney(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetSqlDateTime(int ordinal, SqlDateTime value) { 
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlDateTime(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

#if WINFS_UTC
        public virtual void SetSqlUtcDateTime(int ordinal, SqlUtcDateTime value) {
            ValueUtilsSmi.SetSqlUtcDateTime( _eventSink, _recordBuffer, ordinal, GetSmiMetaData( ordinal ), value ); 
        }
 
        public virtual void SetSqlDate(int ordinal, SqlDate value) { 
            ValueUtilsSmi.SetSqlDate( _eventSink, _recordBuffer, ordinal, GetSmiMetaData( ordinal ), value );
        } 

        public virtual void SetSqlTime(int ordinal, SqlTime value) {
            ValueUtilsSmi.SetSqlTime( _eventSink, _recordBuffer, ordinal, GetSmiMetaData( ordinal ), value );
        } 
#endif
 
        public virtual void SetSqlXml(int ordinal, SqlXml value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlXml(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetSqlDecimal(int ordinal, SqlDecimal value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlDecimal(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetSqlString(int ordinal, SqlString value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlString(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }

        public virtual void SetSqlBinary(int ordinal, SqlBinary value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlBinary(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        } 

        public virtual void SetSqlGuid(int ordinal, SqlGuid value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlGuid(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }
 
        public virtual void SetSqlChars(int ordinal, SqlChars value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlChars(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetSqlBytes(int ordinal, SqlBytes value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlBytes(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

 
        // 
        //  SqlDataRecord public API
        // 

        public SqlDataRecord( params SqlMetaData[] metaData ) {
            // Initial consistency check
            if ( null == metaData ) { 
                throw ADP.ArgumentNull( "metadata" );
            } 
 
            _columnMetaData = new SqlMetaData[metaData.Length];
            _columnSmiMetaData = new SmiExtendedMetaData[metaData.Length]; 
            ulong smiVersion = SmiVersion;
            for (int i = 0; i < _columnSmiMetaData.Length; i++) {
                if ( null == metaData[i] ) {
                    throw ADP.ArgumentNull( "metadata["+i+"]" ); 
                }
                _columnMetaData[i] = metaData[i]; 
                _columnSmiMetaData[i] = MetaDataUtilsSmi.SqlMetaDataToSmiExtendedMetaData( _columnMetaData[i] ); 
                if (!MetaDataUtilsSmi.IsValidForSmiVersion(_columnSmiMetaData[i], smiVersion)) {
                    throw ADP.VersionDoesNotSupportDataType(_columnSmiMetaData[i].TypeName); 
                }
            }

            _eventSink = new SmiEventSink_Default( ); 

            if (InOutOfProcHelper.InProc) { 
                _recordContext = SmiContextFactory.Instance.GetCurrentContext(); 
                _recordBuffer = _recordContext.CreateRecordBuffer( _columnSmiMetaData, _eventSink );
                _usesStringStorageForXml = false; 
            }
            else {
                _recordContext = null;
                _recordBuffer = new MemoryRecordBuffer(_columnSmiMetaData); 
                _usesStringStorageForXml = true;
            } 
            _eventSink.ProcessMessagesAndThrow(); 
        }
 
        internal SqlDataRecord( SmiRecordBuffer recordBuffer, params SmiExtendedMetaData[] metaData ) {
            Debug.Assert(null != recordBuffer, "invalid attempt to instantiate SqlDataRecord with null SmiRecordBuffer");
            Debug.Assert(null != metaData, "invalid attempt to instantiate SqlDataRecord with null SmiExtendedMetaData[]");
 
            _columnMetaData = new SqlMetaData[metaData.Length];
            _columnSmiMetaData = new SmiExtendedMetaData[metaData.Length]; 
            for (int i = 0; i < _columnSmiMetaData.Length; i++) { 
                _columnSmiMetaData[i] = metaData[i];
                _columnMetaData[i] = MetaDataUtilsSmi.SmiExtendedMetaDataToSqlMetaData( _columnSmiMetaData[i] ); 
            }

            _eventSink = new SmiEventSink_Default( );
 
            if (InOutOfProcHelper.InProc) {
                _recordContext = SmiContextFactory.Instance.GetCurrentContext(); 
            } 
            else {
                _recordContext = null; 
            }
            _recordBuffer = recordBuffer;
            _eventSink.ProcessMessagesAndThrow();
        } 

        // 
        //  SqlDataRecord private members 
        //
        internal SmiRecordBuffer RecordBuffer {  // used by SqlPipe 
            get {
                return _recordBuffer;
            }
        } 

        internal SmiContext RecordContext { 
            get { 
                return _recordContext;
            } 
        }

        private ulong SmiVersion {
            get { 
                return InOutOfProcHelper.InProc ? SmiContextFactory.Instance.NegotiatedSmiVersion : SmiContextFactory.LatestVersion;
            } 
        } 

        internal SqlMetaData[] InternalGetMetaData() { 
            return _columnMetaData;
        }

        internal SmiExtendedMetaData[] InternalGetSmiMetaData() { 
            return _columnSmiMetaData;
        } 
 
        internal SmiExtendedMetaData GetSmiMetaData( int ordinal ) {
            return _columnSmiMetaData[ordinal]; 
        }

        internal void ThrowIfInvalidOrdinal( int ordinal ) {
            if ( 0 > ordinal || FieldCount <= ordinal ) { 
                throw ADP.IndexOutOfRange( ordinal );
            } 
        } 
        private void EnsureSubclassOverride() {
            if (null == _recordBuffer) { 
                throw SQL.SubclassMustOverride();
            }
        }
    } 
}
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// [....] 
// [....]
// [....] 
// [....] 
//-----------------------------------------------------------------------------
 
namespace Microsoft.SqlServer.Server {

    using System;
    using System.Data; 
    using System.Data.Common;
    using System.Data.ProviderBase; 
    using System.Data.Sql; 
    using System.Data.SqlTypes;
    using System.Data.SqlClient; 
    using System.Diagnostics;

    public class SqlDataRecord : IDataRecord {
        SmiRecordBuffer         _recordBuffer; 
        SmiContext              _recordContext;
        SmiExtendedMetaData[]   _columnSmiMetaData; 
        SmiEventSink_Default    _eventSink; 
        SqlMetaData[]           _columnMetaData;
        FieldNameLookup         _fieldNameLookup; 
        bool                    _usesStringStorageForXml;

        static readonly SmiMetaData __maxNVarCharForXml = new SmiMetaData(SqlDbType.NVarChar, SmiMetaData.UnlimitedMaxLengthIndicator,
                                        SmiMetaData.DefaultNVarChar_NoCollation.Precision, 
                                        SmiMetaData.DefaultNVarChar_NoCollation.Scale,
                                        SmiMetaData.DefaultNVarChar.LocaleId, 
                                        SmiMetaData.DefaultNVarChar.CompareOptions, 
                                        null);
 
        public virtual int FieldCount {
            get {
                EnsureSubclassOverride();
                return _columnMetaData.Length; 
            }
        } 
 
        public virtual String GetName( int ordinal ) {
            EnsureSubclassOverride(); 
            return GetSqlMetaData(ordinal).Name;
        }

        public virtual String GetDataTypeName( int ordinal ) { 
            EnsureSubclassOverride();
            SqlMetaData metaData = GetSqlMetaData(ordinal); 
            if ( SqlDbType.Udt == metaData.SqlDbType ) { 
                return metaData.UdtTypeName;
            } 
            else {
                return MetaType.GetMetaTypeFromSqlDbType(metaData.SqlDbType, false).TypeName;
            }
        } 

        public virtual Type GetFieldType( int ordinal ) { 
            EnsureSubclassOverride(); 
            if (SqlDbType.Udt == GetSqlMetaData(ordinal).SqlDbType) {
                return GetSqlMetaData( ordinal ).Type; 
            }
            else {
                SqlMetaData md = GetSqlMetaData(ordinal);
                return MetaType.GetMetaTypeFromSqlDbType(md.SqlDbType, false).ClassType; 
            }
        } 
 
        public virtual Object GetValue(int ordinal) {
            EnsureSubclassOverride(); 
            SmiMetaData metaData = GetSmiMetaData(ordinal);

            if (SmiVersion >= SmiContextFactory.KatmaiVersion) {
                return ValueUtilsSmi.GetValue200( 
                                _eventSink,
                                _recordBuffer, 
                                ordinal, 
                                metaData,
                                _recordContext 
                                );
            }
            else {
                return ValueUtilsSmi.GetValue( 
                                _eventSink,
                                (ITypedGettersV3)_recordBuffer, 
                                ordinal, 
                                metaData,
                                _recordContext 
                                );
            }
        }
 
        public virtual int GetValues( object[] values ) {
            EnsureSubclassOverride(); 
            if (null == values) { 
                throw ADP.ArgumentNull("values");
            } 

            int copyLength = ( values.Length < FieldCount ) ? values.Length : FieldCount;
            for(int i=0; i= SmiContextFactory.KatmaiVersion) {
                return ValueUtilsSmi.GetSqlValue200(_eventSink, _recordBuffer, ordinal, metaData, _recordContext); 
            }
            return ValueUtilsSmi.GetSqlValue( _eventSink, _recordBuffer, ordinal, metaData, _recordContext );
        }
 
        public virtual int GetSqlValues(object[] values) {
            EnsureSubclassOverride(); 
            if (null == values) { 
                throw ADP.ArgumentNull("values");
            } 


            int copyLength = (values.Length < FieldCount) ? values.Length : FieldCount;
            for(int i=0; i FieldCount ) ? FieldCount : values.Length; 

            ExtendedClrTypeCode[] typeCodes = new ExtendedClrTypeCode [copyLength]; 

            // Verify all data values as acceptable before changing current state.
            for (int i=0; i < copyLength; i++) {
                SqlMetaData metaData = GetSqlMetaData(i); 
                typeCodes[i] = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
                    metaData.SqlDbType, false /* isMultiValued */, values[i], metaData.Type, SmiVersion); 
                if ( ExtendedClrTypeCode.Invalid == typeCodes[i] ) { 
                    throw ADP.InvalidCast();
                } 
            }

            // Now move the data (it'll only throw if someone plays with the values array between
            //      the validation loop and here, or if an invalid UDT was sent). 
            for( int i=0; i < copyLength; i++ ) {
                if (SmiVersion >= SmiContextFactory.KatmaiVersion) { 
                    ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0, 0, null); 
                }
                else { 
                    ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, i, GetSmiMetaData(i), values[i], typeCodes[i], 0);
                }
            }
 
            return copyLength;
        } 
 
        public virtual void SetValue( int ordinal, object value ) {
            EnsureSubclassOverride(); 
            SqlMetaData metaData = GetSqlMetaData(ordinal);
            ExtendedClrTypeCode typeCode = MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
                        metaData.SqlDbType, false /* isMultiValued */, value, metaData.Type, SmiVersion);
            if ( ExtendedClrTypeCode.Invalid == typeCode ) { 
                    throw ADP.InvalidCast();
            } 
 
            if (SmiVersion >= SmiContextFactory.KatmaiVersion) {
                ValueUtilsSmi.SetCompatibleValueV200(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, 0, 0, null); 
            }
            else {
                ValueUtilsSmi.SetCompatibleValue(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, typeCode, 0);
            } 
        }
 
        public virtual void SetBoolean(int ordinal, bool value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetBoolean(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetByte(int ordinal, byte value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetByte(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetBytes(int ordinal, long fieldOffset, byte[] buffer, int bufferOffset, int length) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetBytes(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), fieldOffset, buffer, bufferOffset, length);
        }

        public virtual void SetChar(int ordinal,  char value) { 
            EnsureSubclassOverride();
            throw ADP.NotSupported(); 
        } 

        public virtual void SetChars(int ordinal, long fieldOffset, char[] buffer, int bufferOffset, int length) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetChars(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), fieldOffset, buffer, bufferOffset, length);
        }
 
        public virtual void SetInt16(int ordinal,  System.Int16 value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetInt16(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetInt32(int ordinal, System.Int32 value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetInt32(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetInt64(int ordinal, System.Int64 value) { 
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetInt64(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetFloat(int ordinal, float value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSingle(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetDouble(int ordinal,  double value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetDouble(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetString(int ordinal, string value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetString(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetDecimal(int ordinal,  Decimal value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetDecimal(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }

        public virtual void SetDateTime(int ordinal,  DateTime value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetDateTime(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        } 

        public virtual void SetTimeSpan(int ordinal, TimeSpan value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetTimeSpan(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, SmiVersion >= SmiContextFactory.KatmaiVersion);
        }
 
        public virtual void SetDateTimeOffset(int ordinal, DateTimeOffset value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetDateTimeOffset(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value, SmiVersion >= SmiContextFactory.KatmaiVersion); 
        }
 
        public virtual void SetDBNull(int ordinal) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetDBNull(_eventSink, _recordBuffer, ordinal, true);
        } 

        public virtual void SetGuid(int ordinal, Guid value) { 
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetGuid(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetSqlBoolean(int ordinal, SqlBoolean value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlBoolean(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetSqlByte(int ordinal, SqlByte value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlByte(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetSqlInt16(int ordinal, SqlInt16 value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlInt16(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetSqlInt32(int ordinal, SqlInt32 value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlInt32(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }

        public virtual void SetSqlInt64(int ordinal, SqlInt64 value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlInt64(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        } 

        public virtual void SetSqlSingle(int ordinal, SqlSingle value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlSingle(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }
 
        public virtual void SetSqlDouble(int ordinal, SqlDouble value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlDouble(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetSqlMoney(int ordinal, SqlMoney value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlMoney(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

        public virtual void SetSqlDateTime(int ordinal, SqlDateTime value) { 
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlDateTime(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

#if WINFS_UTC
        public virtual void SetSqlUtcDateTime(int ordinal, SqlUtcDateTime value) {
            ValueUtilsSmi.SetSqlUtcDateTime( _eventSink, _recordBuffer, ordinal, GetSmiMetaData( ordinal ), value ); 
        }
 
        public virtual void SetSqlDate(int ordinal, SqlDate value) { 
            ValueUtilsSmi.SetSqlDate( _eventSink, _recordBuffer, ordinal, GetSmiMetaData( ordinal ), value );
        } 

        public virtual void SetSqlTime(int ordinal, SqlTime value) {
            ValueUtilsSmi.SetSqlTime( _eventSink, _recordBuffer, ordinal, GetSmiMetaData( ordinal ), value );
        } 
#endif
 
        public virtual void SetSqlXml(int ordinal, SqlXml value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlXml(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }

        public virtual void SetSqlDecimal(int ordinal, SqlDecimal value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlDecimal(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 
 
        public virtual void SetSqlString(int ordinal, SqlString value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlString(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }

        public virtual void SetSqlBinary(int ordinal, SqlBinary value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlBinary(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        } 

        public virtual void SetSqlGuid(int ordinal, SqlGuid value) { 
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlGuid(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        }
 
        public virtual void SetSqlChars(int ordinal, SqlChars value) {
            EnsureSubclassOverride(); 
            ValueUtilsSmi.SetSqlChars(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value); 
        }
 
        public virtual void SetSqlBytes(int ordinal, SqlBytes value) {
            EnsureSubclassOverride();
            ValueUtilsSmi.SetSqlBytes(_eventSink, _recordBuffer, ordinal, GetSmiMetaData(ordinal), value);
        } 

 
        // 
        //  SqlDataRecord public API
        // 

        public SqlDataRecord( params SqlMetaData[] metaData ) {
            // Initial consistency check
            if ( null == metaData ) { 
                throw ADP.ArgumentNull( "metadata" );
            } 
 
            _columnMetaData = new SqlMetaData[metaData.Length];
            _columnSmiMetaData = new SmiExtendedMetaData[metaData.Length]; 
            ulong smiVersion = SmiVersion;
            for (int i = 0; i < _columnSmiMetaData.Length; i++) {
                if ( null == metaData[i] ) {
                    throw ADP.ArgumentNull( "metadata["+i+"]" ); 
                }
                _columnMetaData[i] = metaData[i]; 
                _columnSmiMetaData[i] = MetaDataUtilsSmi.SqlMetaDataToSmiExtendedMetaData( _columnMetaData[i] ); 
                if (!MetaDataUtilsSmi.IsValidForSmiVersion(_columnSmiMetaData[i], smiVersion)) {
                    throw ADP.VersionDoesNotSupportDataType(_columnSmiMetaData[i].TypeName); 
                }
            }

            _eventSink = new SmiEventSink_Default( ); 

            if (InOutOfProcHelper.InProc) { 
                _recordContext = SmiContextFactory.Instance.GetCurrentContext(); 
                _recordBuffer = _recordContext.CreateRecordBuffer( _columnSmiMetaData, _eventSink );
                _usesStringStorageForXml = false; 
            }
            else {
                _recordContext = null;
                _recordBuffer = new MemoryRecordBuffer(_columnSmiMetaData); 
                _usesStringStorageForXml = true;
            } 
            _eventSink.ProcessMessagesAndThrow(); 
        }
 
        internal SqlDataRecord( SmiRecordBuffer recordBuffer, params SmiExtendedMetaData[] metaData ) {
            Debug.Assert(null != recordBuffer, "invalid attempt to instantiate SqlDataRecord with null SmiRecordBuffer");
            Debug.Assert(null != metaData, "invalid attempt to instantiate SqlDataRecord with null SmiExtendedMetaData[]");
 
            _columnMetaData = new SqlMetaData[metaData.Length];
            _columnSmiMetaData = new SmiExtendedMetaData[metaData.Length]; 
            for (int i = 0; i < _columnSmiMetaData.Length; i++) { 
                _columnSmiMetaData[i] = metaData[i];
                _columnMetaData[i] = MetaDataUtilsSmi.SmiExtendedMetaDataToSqlMetaData( _columnSmiMetaData[i] ); 
            }

            _eventSink = new SmiEventSink_Default( );
 
            if (InOutOfProcHelper.InProc) {
                _recordContext = SmiContextFactory.Instance.GetCurrentContext(); 
            } 
            else {
                _recordContext = null; 
            }
            _recordBuffer = recordBuffer;
            _eventSink.ProcessMessagesAndThrow();
        } 

        // 
        //  SqlDataRecord private members 
        //
        internal SmiRecordBuffer RecordBuffer {  // used by SqlPipe 
            get {
                return _recordBuffer;
            }
        } 

        internal SmiContext RecordContext { 
            get { 
                return _recordContext;
            } 
        }

        private ulong SmiVersion {
            get { 
                return InOutOfProcHelper.InProc ? SmiContextFactory.Instance.NegotiatedSmiVersion : SmiContextFactory.LatestVersion;
            } 
        } 

        internal SqlMetaData[] InternalGetMetaData() { 
            return _columnMetaData;
        }

        internal SmiExtendedMetaData[] InternalGetSmiMetaData() { 
            return _columnSmiMetaData;
        } 
 
        internal SmiExtendedMetaData GetSmiMetaData( int ordinal ) {
            return _columnSmiMetaData[ordinal]; 
        }

        internal void ThrowIfInvalidOrdinal( int ordinal ) {
            if ( 0 > ordinal || FieldCount <= ordinal ) { 
                throw ADP.IndexOutOfRange( ordinal );
            } 
        } 
        private void EnsureSubclassOverride() {
            if (null == _recordBuffer) { 
                throw SQL.SubclassMustOverride();
            }
        }
    } 
}
 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK