FromDateTime() public static méthode

public static FromDateTime ( System.DateTime dateTime, byte cb ) : TdsDateTime
dateTime System.DateTime
cb byte
Résultat TdsDateTime
Exemple #1
0
        // valid for DateTime, SmallDateTime, Date, DateTime2
        internal void SetDateTime(DateTime value)
        {
            Debug.Assert(
                SmiXetterAccessMap.IsSetterAccessValid(_metaData, SmiXetterTypeCode.XetDateTime));
            if (SqlDbType.Variant == _metaData.SqlDbType)
            {
                if ((_variantType != null) && (_variantType.SqlDbType == SqlDbType.DateTime2))
                {
                    _stateObj.Parser.WriteSqlVariantDateTime2(value, _stateObj);
                }
                else if ((_variantType != null) && (_variantType.SqlDbType == SqlDbType.Date))
                {
                    _stateObj.Parser.WriteSqlVariantDate(value, _stateObj);
                }
                else
                {
                    TdsDateTime dt = MetaType.FromDateTime(value, 8);
                    _stateObj.Parser.WriteSqlVariantHeader(10, TdsEnums.SQLDATETIME, 0, _stateObj);
                    _stateObj.Parser.WriteInt(dt.days, _stateObj);
                    _stateObj.Parser.WriteInt(dt.time, _stateObj);
                }

                // Clean the variant metadata to prevent sharing it with next row.
                // As a reminder, SetVariantType raises an assert if _variantType is not clean
                _variantType = null;
            }
            else
            {
                _stateObj.WriteByte((byte)_metaData.MaxLength);
                if (SqlDbType.SmallDateTime == _metaData.SqlDbType)
                {
                    TdsDateTime dt = MetaType.FromDateTime(value, (byte)_metaData.MaxLength);
                    Debug.Assert(0 <= dt.days && dt.days <= ushort.MaxValue, "Invalid DateTime '" + value + "' for SmallDateTime");

                    _stateObj.Parser.WriteShort(dt.days, _stateObj);
                    _stateObj.Parser.WriteShort(dt.time, _stateObj);
                }
                else if (SqlDbType.DateTime == _metaData.SqlDbType)
                {
                    TdsDateTime dt = MetaType.FromDateTime(value, (byte)_metaData.MaxLength);
                    _stateObj.Parser.WriteInt(dt.days, _stateObj);
                    _stateObj.Parser.WriteInt(dt.time, _stateObj);
                }
                else
                { // date and datetime2
                    int days = value.Subtract(DateTime.MinValue).Days;
                    if (SqlDbType.DateTime2 == _metaData.SqlDbType)
                    {
                        long time = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[_metaData.Scale];
                        _stateObj.WriteByteArray(BitConverter.GetBytes(time), (int)_metaData.MaxLength - 3, 0);
                    }
                    _stateObj.WriteByteArray(BitConverter.GetBytes(days), 3, 0);
                }
            }
        }
 internal void SetDateTime(DateTime value)
 {
     if (SqlDbType.Variant == this._metaData.SqlDbType)
     {
         TdsDateTime time3 = MetaType.FromDateTime(value, 8);
         this._stateObj.Parser.WriteSqlVariantHeader(10, 0x3d, 0, this._stateObj);
         this._stateObj.Parser.WriteInt(time3.days, this._stateObj);
         this._stateObj.Parser.WriteInt(time3.time, this._stateObj);
     }
     else
     {
         this._stateObj.Parser.WriteByte((byte)this._metaData.MaxLength, this._stateObj);
         if (SqlDbType.SmallDateTime == this._metaData.SqlDbType)
         {
             TdsDateTime time2 = MetaType.FromDateTime(value, (byte)this._metaData.MaxLength);
             this._stateObj.Parser.WriteShort(time2.days, this._stateObj);
             this._stateObj.Parser.WriteShort(time2.time, this._stateObj);
         }
         else if (SqlDbType.DateTime == this._metaData.SqlDbType)
         {
             TdsDateTime time = MetaType.FromDateTime(value, (byte)this._metaData.MaxLength);
             this._stateObj.Parser.WriteInt(time.days, this._stateObj);
             this._stateObj.Parser.WriteInt(time.time, this._stateObj);
         }
         else
         {
             int days = value.Subtract(DateTime.MinValue).Days;
             if (SqlDbType.DateTime2 == this._metaData.SqlDbType)
             {
                 long num = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[this._metaData.Scale];
                 this._stateObj.Parser.WriteByteArray(BitConverter.GetBytes(num), ((int)this._metaData.MaxLength) - 3, 0, this._stateObj);
             }
             this._stateObj.Parser.WriteByteArray(BitConverter.GetBytes(days), 3, 0, this._stateObj);
         }
     }
 }