Example #1
0
        /// <summary>
        /// Write column data to file.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="length"></param>
        /// <param name="type"></param>
        /// <param name="data"></param>
        internal static void WriteData(BinaryWriter writer, int length, ColumnType[] type, object[] data)
        {
            for (int i = 0; i < length; i++)
            {
                object o = data[i];

                if (o == null)
                {
                    writer.Write((int)ColumnType.Null);
                }
                else
                {
                    ColumnType t = type[i];

                    writer.Write((int)t);

                    switch (t)
                    {
                    case ColumnType.Null:
                        o = null;
                        break;

                    case ColumnType.Float:
                    case ColumnType.Real:
                        writer.Write((float)o);
                        break;

                    case ColumnType.DbDouble:
                        writer.Write((double)o);
                        break;

                    case ColumnType.Bit:
                        writer.Write((bool)o);
                        break;

                    case ColumnType.TinyInt:
                        writer.Write((byte)o);
                        break;

                    case ColumnType.SmallInt:
                        writer.Write((short)o);
                        break;

                    case ColumnType.DbDecimal:
                    case ColumnType.Numeric:
                        writer.Write((Decimal)o);
                        break;

                    case ColumnType.Integer:
                        writer.Write((int)o);
                        break;

                    case ColumnType.BigInt:
                        writer.Write((long)o);
                        break;

                    case ColumnType.Date:
                    case ColumnType.Time:
                    case ColumnType.Timestamp:
                        writer.Write(((DateTime)o).Ticks);
                        break;

                    case ColumnType.Binary:
                    case ColumnType.VarBinary:
                    case ColumnType.LongVarBinary:
                        byte[] b = (byte[])o;
                        writer.Write(b.Length);
                        writer.Write(b);
                        break;

                    case ColumnType.Other:
                        byte[] other = ByteArray.Serialize(o);
                        writer.Write(other.Length);
                        writer.Write(other);
                        break;

                    case ColumnType.UniqueIdentifier:
                        writer.Write(((Guid)o).ToString());
                        break;

                    default:
                        writer.Write(o.ToString());
                        break;
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Gets the size of the stored data.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="l"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        unsafe private static int GetSize(object[] data, int l, ColumnType[] type)
        {
            int s = 0;

            for (int i = 0; i < l; i++)
            {
                object o = data[i];

                s += 4;                    // type

                if (o != null)
                {
                    switch (type[i])
                    {
                    case ColumnType.Char:
                    case ColumnType.LongVarChar:
                    case ColumnType.VarChar:
                    case ColumnType.VarCharIgnoreCase:
                        s += System.Text.Encoding.Unicode.GetByteCount(o.ToString());
                        s += 1;
                        break;

                    case ColumnType.Float:
                    case ColumnType.Real:
                        s += sizeof(float);
                        break;

                    case ColumnType.DbDouble:
                        s += sizeof(double);
                        break;

                    case ColumnType.Bit:
                        s += sizeof(bool);
                        break;

                    case ColumnType.TinyInt:
                        s += sizeof(byte);
                        break;

                    case ColumnType.SmallInt:
                        s += sizeof(short);
                        break;

                    case ColumnType.Integer:
                        s += sizeof(int);
                        break;

                    case ColumnType.DbDecimal:
                    case ColumnType.Numeric:
                        s += sizeof(Decimal);
                        break;

                    case ColumnType.BigInt:
                        s += sizeof(long);
                        break;

                    case ColumnType.Date:
                    case ColumnType.Time:
                    case ColumnType.Timestamp:
                        s += sizeof(long);
                        break;

                    case ColumnType.Binary:
                    case ColumnType.VarBinary:
                    case ColumnType.LongVarBinary:
                        s += 4;
                        s += ((byte[])o).Length;
                        break;

                    case ColumnType.Other:
                        s += 4;
                        s += ByteArray.Serialize(o).Length;
                        break;

                    case ColumnType.UniqueIdentifier:
                        s += sizeof(Guid);
                        break;

                    default:
                        s += System.Text.Encoding.Unicode.GetByteCount(o.ToString());
                        s += 1;
                        break;
                    }
                }
            }

            return(s);
        }
Example #3
0
        /// <summary>
        /// Write column data to file.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="length"></param>
        /// <param name="type"></param>
        /// <param name="data"></param>
        internal static void WriteData(BinaryWriter writer, int length, ColumnType[] type, object[] data)
        {
            for (int i = 0; i < length; i++)
            {
                object o = data[i];

                if (o == null)
                {
                    writer.Write((int)ColumnType.Null);
                }
                else
                {
                    ColumnType t = type[i];

                    writer.Write((int)t);

                    switch (t)
                    {
                    case ColumnType.Null:
                        o = null;
                        break;

                    case ColumnType.Float:
                    case ColumnType.Real:
                        writer.Write((float)o);
                        break;

                    case ColumnType.DbDouble:
                        writer.Write((double)o);
                        break;

                    case ColumnType.Bit:
                        writer.Write((bool)o);
                        break;

                    case ColumnType.TinyInt:
                        writer.Write((byte)o);
                        break;

                    case ColumnType.SmallInt:
                        writer.Write((short)o);
                        break;

                    case ColumnType.DbDecimal:
                    case ColumnType.Numeric:
                                                        #if !POCKETPC
                        writer.Write((Decimal)o);
                                                        #else
                        int[]  bits  = Decimal.GetBits((Decimal)o);
                        byte[] bytes = new byte[bits.Length * 4];
                        for (int ix = 0; ix < bits.Length; ix++)
                        {
                            byte[] r = BitConverter.GetBytes(bits[ix]);
                            Array.Copy(r, 0, bytes, (ix * 4), 4);
                        }
                        writer.Write(bytes.Length);
                        writer.Write(bytes);
                                                        #endif
                        break;

                    case ColumnType.Integer:
                        writer.Write((int)o);
                        break;

                    case ColumnType.BigInt:
                        writer.Write((long)o);
                        break;

                    case ColumnType.Date:
                    case ColumnType.Time:
                    case ColumnType.Timestamp:
                        writer.Write(((DateTime)o).Ticks);
                        break;

                    case ColumnType.Binary:
                    case ColumnType.VarBinary:
                    case ColumnType.LongVarBinary:
                        byte[] b = (byte[])o;
                        writer.Write(b.Length);
                        writer.Write(b);
                        break;

                    case ColumnType.Other:
                        byte[] other = ByteArray.Serialize(o);
                        writer.Write(other.Length);
                        writer.Write(other);
                        break;

                    case ColumnType.UniqueIdentifier:
                        writer.Write(((Guid)o).ToString());
                        break;

                    default:
                        writer.Write(o.ToString());
                        break;
                    }
                }
            }
        }