Ejemplo n.º 1
0
        private void AddOrUpdateRecord(string key, byte[] buffer, DateTime expiration)
        {
            var fieldValues = new FieldValues(3);

            fieldValues.Add(valueField, buffer ?? new byte[0]);
            fieldValues.Add(expiresField, expiration);

            try
            {
                if (fileDb.GetRecordByKey(key, new string[0], false) != null)
                {
                    fileDb.UpdateRecordByKey(key, fieldValues);
                }
                else
                {
                    fieldValues.Add(keyField, key);
                    fileDb.AddRecord(fieldValues);
                }

                //Debug.WriteLine("FileDbCache: Writing \"{0}\", Expires {1}", key, imageCacheItem.Expiration.ToLocalTime());
            }
            catch (Exception ex)
            {
                Debug.WriteLine("FileDbCache.AddOrUpdateRecord(\"{0}\"): {1}", key, ex.Message); return;
            }
        }
Ejemplo n.º 2
0
        private bool AddOrUpdateRecord(string key, object value, DateTime?expiration = null)
        {
            var fieldValues = new FieldValues(3);

            fieldValues.Add(valueField, value);

            if (expiration != null)
            {
                fieldValues.Add(expiresField, expiration.Value);
            }

            bool recordExists;

            try
            {
                recordExists = fileDb.GetRecordByKey(key, new string[0], false) != null;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("FileDbCache: FileDb.GetRecordByKey(\"{0}\"): {1}", key, ex.Message);
                return(false);
            }

            if (recordExists)
            {
                try
                {
                    fileDb.UpdateRecordByKey(key, fieldValues);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("FileDbCache: FileDb.UpdateRecordByKey(\"{0}\"): {1}", key, ex.Message);
                    return(false);
                }
            }
            else
            {
                try
                {
                    fieldValues.Add(keyField, key);
                    fileDb.AddRecord(fieldValues);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("FileDbCache: FileDb.AddRecord(\"{0}\"): {1}", key, ex.Message);
                    return(false);
                }
            }

            //Debug.WriteLine("FileDbCache: Writing \"{0}\", Expires {1}", key, expiration.ToLocalTime());
            return(true);
        }
Ejemplo n.º 3
0
        private int PostData(Book book, FileDb booksDb)
        {
            if (booksDb.IsOpen)
            {
                booksDb.Open("Books.fdb", false);
                var record = new FieldValues();
                record.Add("AuthorsName", book.AuthorsName);
                record.Add("Mvp", book.Mvp);
                record.Add("Title", book.Title);

                return(booksDb.AddRecord(record));
            }
            return(0);
        }
Ejemplo n.º 4
0
        private bool AddOrUpdateRecord(string key, byte[] value, DateTime expires)
        {
            var fieldValues = new FieldValues(3);

            fieldValues.Add(valueField, value);
            fieldValues.Add(expiresField, expires);

            bool recordExists;

            try
            {
                recordExists = fileDb.GetRecordByKey(key, new string[0], false) != null;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("FileDbCache: FileDb.GetRecordByKey(\"{0}\") failed: {1}", key, ex.Message);
                return(false);
            }

            if (recordExists)
            {
                try
                {
                    fileDb.UpdateRecordByKey(key, fieldValues);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("FileDbCache: FileDb.UpdateRecordByKey(\"{0}\") failed: {1}", key, ex.Message);
                    return(false);
                }
            }
            else
            {
                try
                {
                    fieldValues.Add(keyField, key);
                    fileDb.AddRecord(fieldValues);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("FileDbCache: FileDb.AddRecord(\"{0}\") failed: {1}", key, ex.Message);
                    return(false);
                }
            }

            //Debug.WriteLine("Cached item {0}, expires {1}", key, expires);
            return(true);
        }
Ejemplo n.º 5
0
        public void AddRecords()
        {
            FileDb _db = new FileDb();
            _db.Open(DBFILE, false);
            var record = new FieldValues();
            record.Add("FirstName", "Nancy");
            record.Add("LastName", "Davolio");
            record.Add("BirthDate", new DateTime(1968, 12, 8));
            record.Add("IsCitizen", true);
            record.Add("DoubleField", 1.23);
            record.Add("ByteField", 1);
            record.Add("StringArrayField", new string[] { "s1", "s2", "s3" });
            record.Add("ByteArrayField", new Byte[] { 1, 2, 3, 4 });
            record.Add("IntArrayField", new int[] { 100, 200, 300, 400 });
            record.Add("DoubleArrayField", new double[] { 1.2, 2.4, 3.6, 4.8 });
            record.Add("DateTimeArrayField", new DateTime[]
                { DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now });
            record.Add("BoolArrayField", new bool[] { true, false, true, false });
            _db.AddRecord(record);
            _db.Dispose();

        }
Ejemplo n.º 6
0
        public void AddRecords()
        {
            FileDb _db = new FileDb();

            _db.Open(DBFILE, false);
            var record = new FieldValues();

            record.Add("FirstName", "Nancy");
            record.Add("LastName", "Davolio");
            record.Add("BirthDate", new DateTime(1968, 12, 8));
            record.Add("IsCitizen", true);
            record.Add("DoubleField", 1.23);
            record.Add("ByteField", 1);
            record.Add("StringArrayField", new string[] { "s1", "s2", "s3" });
            record.Add("ByteArrayField", new Byte[] { 1, 2, 3, 4 });
            record.Add("IntArrayField", new int[] { 100, 200, 300, 400 });
            record.Add("DoubleArrayField", new double[] { 1.2, 2.4, 3.6, 4.8 });
            record.Add("DateTimeArrayField", new DateTime[]
                       { DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now });
            record.Add("BoolArrayField", new bool[] { true, false, true, false });
            _db.AddRecord(record);
            _db.Dispose();
        }
Ejemplo n.º 7
0
        private void AddOrUpdateRecord(string key, byte[] buffer, DateTime expiration)
        {
            var fieldValues = new FieldValues(3);

            fieldValues.Add(valueField, buffer ?? new byte[0]);
            fieldValues.Add(expiresField, expiration);

            try
            {
                if (fileDb.GetRecordByKey(key, new string[0], false) != null)
                {
                    fileDb.UpdateRecordByKey(key, fieldValues);
                }
                else
                {
                    fieldValues.Add(keyField, key);
                    fileDb.AddRecord(fieldValues);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"FileDbCache.AddOrUpdateRecord({key}): {ex.Message}");
            }
        }
Ejemplo n.º 8
0
        private void BtnAddRecords_Click(object sender, RoutedEventArgs e)
        {
            try
            {
#if true
                Stream s = this.GetType().Assembly.GetManifestResourceStream(this.GetType(), "data.xml");
                string xml;
                using (StreamReader reader = new StreamReader(s))
                {
                    xml = reader.ReadToEnd();
                }

                XDocument xmlDoc = XDocument.Parse(xml);

                // find the columns
                var    rows = xmlDoc.Descendants("row");
                char[] toks = "|".ToCharArray();

                foreach (var row in rows)
                {
                    var columns = from col in row.Descendants("col")
                                  select new
                    {
                        value = col.Value
                    };

                    var record = new FieldValues(20);
                    int idx    = 0;

                    foreach (var col in columns)
                    {
                        string value = (string)col.value;
                        Field  field = _db.Fields[idx];

                        if (!string.IsNullOrEmpty(value))
                        {
                            if (field.DataType == DataTypeEnum.Bool)
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <bool>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(bool.Parse(val));
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    bool bval;
                                    if (bool.TryParse(value, out bval))
                                    {
                                        record.Add(field.Name, bval);
                                    }
                                }
                            }
                            else if (field.DataType == DataTypeEnum.Byte)
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <Byte>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(Byte.Parse(val));
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    Byte bval;
                                    if (Byte.TryParse(value, out bval))
                                    {
                                        record.Add(field.Name, bval);
                                    }
                                }
                            }
                            else if (field.DataType == DataTypeEnum.Int32)
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <Int32>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(Int32.Parse(val));
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    Int32 ival;
                                    if (Int32.TryParse(value, out ival))
                                    {
                                        record.Add(field.Name, ival);
                                    }
                                }
                            }
                            else if (field.DataType == DataTypeEnum.UInt32)
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <UInt32>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(UInt32.Parse(val));
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    UInt32 ival;
                                    if (UInt32.TryParse(value, out ival))
                                    {
                                        record.Add(field.Name, ival);
                                    }
                                }
                            }
                            else if (field.DataType == DataTypeEnum.Float)
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <float>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(float.Parse(val));
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    float dval;
                                    if (float.TryParse(value, out dval))
                                    {
                                        record.Add(field.Name, dval);
                                    }
                                }
                            }
                            else if (field.DataType == DataTypeEnum.Double)
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <double>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(double.Parse(val));
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    double dval;
                                    if (double.TryParse(value, out dval))
                                    {
                                        record.Add(field.Name, dval);
                                    }
                                }
                            }
                            else // DateTime or String - DateTime is convertable to string in FileDb
                            {
                                if (field.IsArray)
                                {
                                    var      list = new List <string>(10);
                                    string[] vals = value.Split(toks);
                                    foreach (string val in vals)
                                    {
                                        list.Add(val);
                                    }
                                    record.Add(field.Name, list.ToArray());
                                }
                                else
                                {
                                    record.Add(field.Name, value);
                                }
                            }
                        }

                        idx++;
                    }

                    _db.AddRecord(record);
                }
#else
                var record = new FieldValues();
                record.Add("FirstName", "Nancy");
                record.Add("LastName", "Davolio");
                record.Add("BirthDate", new DateTime(1968, 12, 8));
                record.Add("IsCitizen", true);
                record.Add("Float", 1.23);
                record.Add("Byte", 1);
                record.Add("StringArray", new string[] { "s1", "s2", "s3" });
                record.Add("ByteArray", new Byte[] { 1, 2, 3, 4 });
                record.Add("IntArray", new int[] { 100, 200, 300, 400 });
                record.Add("FloatArray", new double[] { 1.2, 2.4, 3.6, 4.8 });
                record.Add("DateTimeArray", new DateTime[] { DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now });
                record.Add("BoolArray", new bool[] { true, false, true, false });
                _db.AddRecord(record);

                record = new FieldValues();
                record.Add("FirstName", "Andrew");
                record.Add("LastName", "Fuller");
                record.Add("BirthDate", new DateTime(1962, 1, 12));
                record.Add("IsCitizen", true);
                record.Add("Float", 2.567);
                record.Add("Byte", 2);
                _db.AddRecord(record);

                record = new FieldValues();
                record.Add("FirstName", "Janet");
                record.Add("LastName", "Leverling");
                record.Add("BirthDate", new DateTime(1963, 8, 30));
                record.Add("IsCitizen", false);
                record.Add("Float", 3.14);
                record.Add("Byte", 3);
                _db.AddRecord(record);

                record = new FieldValues();
                record.Add("FirstName", "Margaret");
                record.Add("LastName", "Peacock");
                record.Add("BirthDate", new DateTime(1957, 9, 19));
                record.Add("IsCitizen", false);
                record.Add("Float", 4.96);
                record.Add("Byte", 4);
                _db.AddRecord(record);

                record = new FieldValues();
                record.Add("FirstName", "Steven");
                record.Add("LastName", "Buchanan");
                record.Add("BirthDate", new DateTime(1965, 3, 4));
                record.Add("IsCitizen", true);
                record.Add("Float", 5.7);
                record.Add("Byte", 5);
                _db.AddRecord(record);
#endif
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Add record to the DB
 /// </summary>
 /// <param name="db">The db to add to</param>
 /// <param name="values">The values to add to the table.</param>
 public void AddRecord(FileDb db, FieldValues values)
 {
     db.Open(Name, false);
     db.AddRecord(values);
 }
Ejemplo n.º 10
0
        void create( String fileName )
        {
            // Database schema:
            // ID  ParentID  Name  Type  Value  Values

            Field field;
            List<Field> fieldLst = new List<Field>( 10 );

            field = new Field( StrID, DataTypeEnum.Int );
            field.AutoIncStart = 0;
            field.IsPrimaryKey = true;
            fieldLst.Add( field );

            field = new Field( StrParentID, DataTypeEnum.Int );
            fieldLst.Add( field );

            field = new Field( StrName, DataTypeEnum.String );
            fieldLst.Add( field );

            field = new Field( StrType, DataTypeEnum.Int );
            fieldLst.Add( field );

            field = new Field( StrDataType, DataTypeEnum.Int );
            fieldLst.Add( field );

            field = new Field( StrValue, DataTypeEnum.String );
            fieldLst.Add( field );

            field = new Field( StrValues, DataTypeEnum.String );
            field.IsArray = true;
            fieldLst.Add( field );

            _db = new FileDb();
            _db.Create( fileName, fieldLst.ToArray() );

            FieldValues fieldValues = new FieldValues(3);
            fieldValues.Add( StrParentID, (Int32) (-1) );
            fieldValues.Add( StrName, "LocalMachine" );
            fieldValues.Add( StrType, (Int32) KeyValueType.Key );
            fieldValues.Add( StrDataType, (Int32) ConfigDbDataType.String );
            _db.AddRecord( fieldValues );

            fieldValues = new FieldValues(3);
            fieldValues.Add( StrParentID, (Int32) (-1) );
            fieldValues.Add( StrName, "CurrentUser" );
            fieldValues.Add( StrType, (Int32) KeyValueType.Key );
            fieldValues.Add( StrDataType, (Int32) ConfigDbDataType.String );
            _db.AddRecord( fieldValues );
        }