// Put the column value to the ESE.
        void LoadVal(string strVal, JET_COLUMNID idColumn, JET_coltyp ct, JET_CP cp)
        {
            if (String.IsNullOrEmpty(strVal))
            {
                return;
            }

            byte[] val = null;
            if (ct == JET_coltyp.Currency)                                               // int64
            {
                val = BitConverter.GetBytes(Convert.ToUInt64(strVal, 16));
            }
            else if (ct == JET_coltyp.Long)                                              // int32
            {
                val = BitConverter.GetBytes(Convert.ToUInt32(strVal, 16));
            }
            else if (ct == JET_coltyp.Short)                                             // int16
            {
                val = BitConverter.GetBytes(Convert.ToUInt16(strVal, 16));
            }
            else if (ct == JET_coltyp.UnsignedByte)                                      // uint8
            {
                val = new byte[1] {
                    Convert.ToByte(strVal, 16)
                }
            }
            ;
            else if (ct == JET_coltyp.DateTime)                                          // DateTime
            {
                DateTime dt  = DateTime.Parse(strVal, null, System.Globalization.DateTimeStyles.RoundtripKind);
                double   dbl = dt.ToOADate();
                val = BitConverter.GetBytes(dbl);
            }
            else if (ct == JET_coltyp.Text || ct == JET_coltyp.LongText)                 // Text
            {
                if (cp == JET_CP.Unicode)
                {
                    val = TSV.UnescapeUnicode(strVal).ToArray();
                }
                else
                {
                    val = TSV.UnescapeASCII(strVal).ToArray();
                }
            }
            else
            {
                val = Convert.FromBase64String(strVal);                                  // Anything else
            }
            JET_SETINFO si = new JET_SETINFO();

            si.itagSequence = 0;
            Api.JetSetColumn(sess.idSession, idTable, idColumn, val, val.Length, SetColumnGrbit.None, si);
        }
 /// <summary>
 /// Add a column to the table.
 /// </summary>
 /// <param name="sesid">The session to use.</param>
 /// <param name="tableid">The table to add the column to.</param>
 /// <param name="name">The name of the column.</param>
 /// <param name="coltyp">The type of the column.</param>
 /// <param name="cp">The codepage to use.</param>
 private void AddColumn(JET_SESID sesid, JET_TABLEID tableid, string name, JET_coltyp coltyp, JET_CP cp)
 {
     JET_COLUMNID ignored;
     Api.JetAddColumn(
         sesid,
         tableid,
         name,
         new JET_COLUMNDEF() { coltyp = coltyp, cp = cp },
         null,
         0,
         out ignored);
 }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the ColumnInfo class.
 /// </summary>
 /// <param name="name">Name of the column.</param>
 /// <param name="columnid">ID of the column.</param>
 /// <param name="coltyp">Type of the column.</param>
 /// <param name="cp">Codepage of the column.</param>
 /// <param name="maxLength">Maximum length of the column.</param>
 /// <param name="defaultValue">Column default value.</param>
 /// <param name="grbit">Column option.</param>
 internal ColumnInfo(
     string name,
     JET_COLUMNID columnid,
     JET_coltyp coltyp,
     JET_CP cp,
     int maxLength,
     byte[] defaultValue,
     ColumndefGrbit grbit)
 {
     this.Name = name;
     this.Columnid = columnid;
     this.Coltyp = coltyp;
     this.Cp = cp;
     this.MaxLength = maxLength;
     this.defaultValue = (null == defaultValue) ? null : Array.AsReadOnly(defaultValue);
     this.Grbit = grbit;
 }
Exemple #4
0
 /// <summary>
 /// Initializes a new instance of the ColumnInfo class.
 /// </summary>
 /// <param name="name">Name of the column.</param>
 /// <param name="columnid">ID of the column.</param>
 /// <param name="coltyp">Type of the column.</param>
 /// <param name="cp">Codepage of the column.</param>
 /// <param name="maxLength">Maximum length of the column.</param>
 /// <param name="defaultValue">Column default value.</param>
 /// <param name="grbit">Column option.</param>
 internal ColumnInfo(
     string name,
     JET_COLUMNID columnid,
     JET_coltyp coltyp,
     JET_CP cp,
     int maxLength,
     byte[] defaultValue,
     ColumndefGrbit grbit)
 {
     this.Name         = name;
     this.Columnid     = columnid;
     this.Coltyp       = coltyp;
     this.Cp           = cp;
     this.MaxLength    = maxLength;
     this.DefaultValue = defaultValue;
     this.Grbit        = grbit;
 }
 /// <summary>
 /// Initializes a new instance of the ColumnInfo class.
 /// </summary>
 /// <param name="name">Name of the column.</param>
 /// <param name="columnid">ID of the column.</param>
 /// <param name="coltyp">Type of the column.</param>
 /// <param name="cp">Codepage of the column.</param>
 /// <param name="maxLength">Maximum length of the column.</param>
 /// <param name="defaultValue">Column default value.</param>
 /// <param name="grbit">Column option.</param>
 internal ColumnInfo(
     string name,
     JET_COLUMNID columnid,
     JET_coltyp coltyp,
     JET_CP cp,
     int maxLength,
     byte[] defaultValue,
     ColumndefGrbit grbit)
 {
     this.Name = name;
     this.Columnid = columnid;
     this.Coltyp = coltyp;
     this.Cp = cp;
     this.MaxLength = maxLength;
     this.defaultValue = (null == defaultValue) ? null : new ReadOnlyCollection<byte>(defaultValue);
     this.Grbit = grbit;
 }
Exemple #6
0
 /// <summary>
 /// Initializes a new instance of the ColumnInfo class.
 /// </summary>
 /// <param name="name">Name of the column.</param>
 /// <param name="columnid">ID of the column.</param>
 /// <param name="coltyp">Type of the column.</param>
 /// <param name="cp">Codepage of the column.</param>
 /// <param name="maxLength">Maximum length of the column.</param>
 /// <param name="defaultValue">Column default value.</param>
 /// <param name="grbit">Column option.</param>
 internal ColumnInfo(
     string name,
     JET_COLUMNID columnid,
     JET_coltyp coltyp,
     JET_CP cp,
     int maxLength,
     byte[] defaultValue,
     ColumndefGrbit grbit)
 {
     this.Name = name;
     this.Columnid = columnid;
     this.Coltyp = coltyp;
     this.Cp = cp;
     this.MaxLength = maxLength;
     this.DefaultValue = defaultValue;
     this.Grbit = grbit;
 }
        /// <summary>
        /// Add a column to the table.
        /// </summary>
        /// <param name="sesid">The session to use.</param>
        /// <param name="tableid">The table to add the column to.</param>
        /// <param name="name">The name of the column.</param>
        /// <param name="coltyp">The type of the column.</param>
        /// <param name="cp">The codepage to use.</param>
        private void AddColumn(JET_SESID sesid, JET_TABLEID tableid, string name, JET_coltyp coltyp, JET_CP cp)
        {
            JET_COLUMNID ignored;

            Api.JetAddColumn(
                sesid,
                tableid,
                name,
                new JET_COLUMNDEF()
            {
                coltyp = coltyp, cp = cp
            },
                null,
                0,
                out ignored);
        }
        /// <summary>
        /// Gets random data for a column.
        /// </summary>
        /// <param name="coltyp">The type of the column.</param>
        /// <param name="cp">
        /// The code page of the column. Only used for text columns.
        /// </param>
        /// <param name="rand">Random number generator.</param>
        /// <returns>
        /// Random data for the column.
        /// </returns>
        public static byte[] GetRandomColumnData(JET_coltyp coltyp, JET_CP cp, Random rand)
        {
            byte[] data;

            switch (coltyp)
            {
            case JET_coltyp.Bit:
                data    = new byte[1];
                data[0] = (0 == rand.Next(2)) ? checked ((byte)0x0) : checked ((byte)0xFF);
                break;

            case JET_coltyp.UnsignedByte:
                data = new byte[1];
                rand.NextBytes(data);
                break;

            case JET_coltyp.Short:
                data = new byte[2];
                rand.NextBytes(data);
                break;

            case JET_coltyp.Long:
                data = new byte[4];
                rand.NextBytes(data);
                break;

            case JET_coltyp.Currency:
                data = new byte[8];
                rand.NextBytes(data);
                break;

            case JET_coltyp.IEEESingle:
                data = new byte[4];
                rand.NextBytes(data);
                break;

            case JET_coltyp.IEEEDouble:
                data = new byte[8];
                rand.NextBytes(data);
                break;

            case JET_coltyp.DateTime:
                data = new byte[8];
                rand.NextBytes(data);
                break;

            case JET_coltyp.Binary:
            {
                int size = rand.Next(255) + 1;
                data = GetRandomBinaryBytes(size, rand);
                break;
            }

            case JET_coltyp.Text:
            {
                // GetRandomUnicodeBytes will round up the size of
                // an odd-sized request so a request for 255 bytes will
                // give a 256 byte buffer, which is too large. Restrict
                // unicode columns to 254 bytes.
                int size = (JET_CP.ASCII == cp) ? rand.Next(255) : rand.Next(254) + 1;
                data = (JET_CP.ASCII == cp) ? GetRandomAsciiBytes(size, rand) : GetRandomUnicodeBytes(size, rand);
                break;
            }

            case JET_coltyp.LongBinary:
            {
                int size = rand.Next(9 * 1024) + 1;
                data = GetRandomBinaryBytes(size, rand);
                break;
            }

            case JET_coltyp.LongText:
            {
                int size = rand.Next(9 * 1024) + 1;
                data = (JET_CP.ASCII == cp) ? GetRandomAsciiBytes(size, rand) : GetRandomUnicodeBytes(size, rand);
                break;
            }

            case VistaColtyp.UnsignedLong:
                data = new byte[4];
                rand.NextBytes(data);
                break;

            case VistaColtyp.LongLong:
                data = new byte[8];
                rand.NextBytes(data);
                break;

            case VistaColtyp.GUID:
                data = new byte[16];
                rand.NextBytes(data);
                break;

            case VistaColtyp.UnsignedShort:
                data = new byte[2];
                rand.NextBytes(data);
                break;

            default:
                throw new Exception("Invalid coltyp");
            }

            return(data);
        }
Exemple #9
0
        /// <summary>
        /// Gets random data for a column.
        /// </summary>
        /// <param name="coltyp">The type of the column.</param>
        /// <param name="cp">
        /// The code page of the column. Only used for text columns.
        /// </param>
        /// <param name="rand">Random number generator.</param>
        /// <returns>
        /// Random data for the column.
        /// </returns>
        public static byte[] GetRandomColumnData(JET_coltyp coltyp, JET_CP cp, Random rand)
        {
            byte[] data;

            switch (coltyp)
            {
                case JET_coltyp.Bit:
                    data = new byte[1];
                    data[0] = (0 == rand.Next(2)) ? checked((byte)0x0) : checked((byte)0xFF);
                    break;
                case JET_coltyp.UnsignedByte:
                    data = new byte[1];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.Short:
                    data = new byte[2];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.Long:
                    data = new byte[4];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.Currency:
                    data = new byte[8];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.IEEESingle:
                    data = new byte[4];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.IEEEDouble:
                    data = new byte[8];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.DateTime:
                    data = new byte[8];
                    rand.NextBytes(data);
                    break;
                case JET_coltyp.Binary:
                {
                    int size = rand.Next(255) + 1;
                    data = GetRandomBinaryBytes(size, rand);
                    break;
                }

                case JET_coltyp.Text:
                {
                    // GetRandomUnicodeBytes will round up the size of
                    // an odd-sized request so a request for 255 bytes will
                    // give a 256 byte buffer, which is too large. Restrict
                    // unicode columns to 254 bytes.
                    int size = (JET_CP.ASCII == cp) ? rand.Next(255) : rand.Next(254) + 1;
                    data = (JET_CP.ASCII == cp) ? GetRandomAsciiBytes(size, rand) : GetRandomUnicodeBytes(size, rand);
                    break;
                }

                case JET_coltyp.LongBinary:
                {
                    int size = rand.Next(9 * 1024) + 1;
                    data = GetRandomBinaryBytes(size, rand);
                    break;
                }

                case JET_coltyp.LongText:
                {
                    int size = rand.Next(9 * 1024) + 1;
                    data = (JET_CP.ASCII == cp) ? GetRandomAsciiBytes(size, rand) : GetRandomUnicodeBytes(size, rand);
                    break;
                }

                case VistaColtyp.UnsignedLong:
                    data = new byte[4];
                    rand.NextBytes(data);
                    break;
                case VistaColtyp.LongLong:
                    data = new byte[8];
                    rand.NextBytes(data);
                    break;
                case VistaColtyp.GUID:
                    data = new byte[16];
                    rand.NextBytes(data);
                    break;
                case VistaColtyp.UnsignedShort:
                    data = new byte[2];
                    rand.NextBytes(data);
                    break;
                default:
                    throw new Exception("Invalid coltyp");
            }

            return data;
        }
        // Retrieve the column value from the ESE.
        // Returns the properly-escaped string suitable for TSV file.
        string getStringValue(TextWriter bw, JET_COLUMNID idColumn, int itagSequence, JET_coltyp ct, JET_CP cp)
        {
            JET_RETINFO ri = new JET_RETINFO();

            ri.itagSequence = itagSequence;

            int cbSize;

            Api.JetRetrieveColumn(sess.idSession, idTable, idColumn,
                                  buff, buff.Length, out cbSize, RetrieveColumnGrbit.None, ri);
            if (cbSize <= 0)
            {
                return("");
            }

            if (ct == JET_coltyp.Currency)                       // int64
            {
                Debug.Assert(8 == cbSize);
                return(BitConverter.ToUInt64(buff, 0).ToString("X16"));
            }

            if (ct == JET_coltyp.Long)                       // int32
            {
                Debug.Assert(4 == cbSize);
                return(BitConverter.ToUInt32(buff, 0).ToString("X8"));
            }

            if (ct == JET_coltyp.Short)                      // int16
            {
                Debug.Assert(2 == cbSize);
                return(BitConverter.ToUInt16(buff, 0).ToString("X4"));
            }

            if (ct == JET_coltyp.UnsignedByte)               // uint8
            {
                Debug.Assert(1 == cbSize);
                return(buff[0].ToString("X2"));
            }

            if (ct == JET_coltyp.DateTime)                   // DateTime
            {
                Debug.Assert(8 == cbSize);
                double   dbl = BitConverter.ToDouble(buff, 0);
                DateTime dt  = Conversions.ConvertDoubleToDateTime(dbl);
                return(dt.ToString("o"));
            }

            if (ct == JET_coltyp.Text || ct == JET_coltyp.LongText)              // Text
            {
                if (cp == JET_CP.Unicode)
                {
                    return(TSV.EscapeUnicode(buff, cbSize));
                }
                else
                {
                    return(TSV.EscapeASCII(buff, cbSize));
                }
            }
            return(Convert.ToBase64String(buff, 0, cbSize));                     // Anything else
        }
        public static void AddColumn(JET_SESID session, JET_TABLEID tableId, string name, JET_coltyp type, JET_CP cp, ColumndefGrbit grBit)
        {
            JET_COLUMNID columnid;

            Api.JetAddColumn(session, tableId, name, new JET_COLUMNDEF { coltyp = type, cp = cp, grbit = grBit }, null, 0, out columnid);
        }