示例#1
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object value, int length)
        {
            MySqlDateTime dtValue;

            if (value is DateTime)
            {
                dtValue = new MySqlDateTime(type, (DateTime)value);
            }
            else if (value is string)
            {
                dtValue = new MySqlDateTime(type, DateTime.Parse((string)value,
                                                                 System.Globalization.CultureInfo.CurrentCulture));
            }
            else if (value is MySqlDateTime)
            {
                dtValue = (MySqlDateTime)value;
            }
            else
            {
                throw new MySqlException("Unable to serialize date/time value.");
            }

            if (!binary)
            {
                SerializeText(stream, dtValue);
                return;
            }

            if (type == MySqlDbType.Timestamp)
            {
                stream.WriteByte(11);
            }
            else
            {
                stream.WriteByte(7);
            }

            stream.WriteInteger(dtValue.Year, 2);
            stream.WriteByte((byte)dtValue.Month);
            stream.WriteByte((byte)dtValue.Day);
            if (type == MySqlDbType.Date)
            {
                stream.WriteByte(0);
                stream.WriteByte(0);
                stream.WriteByte(0);
            }
            else
            {
                stream.WriteByte((byte)dtValue.Hour);
                stream.WriteByte((byte)dtValue.Minute);
                stream.WriteByte((byte)dtValue.Second);
            }

            if (type == MySqlDbType.Timestamp)
            {
                stream.WriteInteger(dtValue.Millisecond, 4);
            }
        }
示例#2
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            if (!(val is TimeSpan))
            {
                throw new MySqlException("Only TimeSpan objects can be serialized by MySqlTimeSpan");
            }

            TimeSpan ts       = (TimeSpan)val;
            bool     negative = ts.TotalMilliseconds < 0;

            ts = ts.Duration();

            if (binary)
            {
                stream.WriteByte(8);
                stream.WriteByte((byte)(negative ? 1 : 0));
                stream.WriteInteger(ts.Days, 4);
                stream.WriteByte((byte)ts.Hours);
                stream.WriteByte((byte)ts.Minutes);
                stream.WriteByte((byte)ts.Seconds);
            }
            else
            {
                String s = String.Format("'{0}{1} {2:00}:{3:00}:{4:00}.{5}'",
                                         negative ? "-" : "", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);

                stream.WriteStringNoNull(s);
            }
        }
示例#3
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            if (!(val is TimeSpan))
            {
                throw new MySqlException("Only TimeSpan objects can be serialized by MySqlTimeSpan");
            }
            TimeSpan span = (TimeSpan)val;
            bool     flag = span.TotalMilliseconds < 0.0;

            span = span.Duration();
            if (binary)
            {
                stream.WriteByte(8);
                stream.WriteByte(flag ? ((byte)1) : ((byte)0));
                stream.WriteInteger((long)span.Days, 4);
                stream.WriteByte((byte)span.Hours);
                stream.WriteByte((byte)span.Minutes);
                stream.WriteByte((byte)span.Seconds);
            }
            else
            {
                string v = string.Format("'{0}{1} {2:00}:{3:00}:{4:00}.{5}'", new object[] { flag ? "-" : "", span.Days, span.Hours, span.Minutes, span.Seconds, span.Milliseconds });
                stream.WriteStringNoNull(v);
            }
        }