コード例 #1
0
        // load databases from files vendors.txt and inventory.txt
        private void LoadBtn_Click(object sender, EventArgs e)
        {
            if (errStream != null)
            {
                errStream.Close();
            }
            Databases dbs     = Databases.Instance;
            string    dbPath  = Path.Combine(Application.StartupPath, "Db");
            string    appName = Path.GetFileName(Application.ExecutablePath);

            errStream = File.Open(Path.Combine(dbPath, "errors.txt"), FileMode.OpenOrCreate, FileAccess.Write);
            dbs.Open(dbPath, appName, errStream);

            // load vendors
            StreamReader sr     = new StreamReader(Path.Combine(dbPath, "vendors.txt"));
            Regex        rgx    = new Regex("(.*)#(.*)#(.*)#(.*)#(.*)#(.*)#(.*)#(.*)", RegexOptions.Compiled);
            Vendor       vendor = new Vendor();
            string       line;
            DbEntry      keyEntry;
            DbEntry      dataEntry;

            while ((line = sr.ReadLine()) != null && line.Length > 0)
            {
                Match           match  = rgx.Match(line);
                GroupCollection groups = match.Groups;
                if (groups.Count < 9)
                {
                    throw new ApplicationException("Input error on vendors.txt");
                }
                InitVendorFromRegex(vendor, groups);
                keyEntry  = dbs.Fmt.ToDbEntry <string>(vendor.Name);
                dataEntry = dbs.Fmt.ToDbEntry <Vendor>(vendor);
                WriteStatus status = dbs.VendorDb.Put(null, ref keyEntry, ref dataEntry);
                if (status != WriteStatus.Success)
                {
                    throw new ApplicationException("Put failed");
                }
            }

            // load inventory
            sr  = new StreamReader(Path.Combine(dbPath, "inventory.txt"));
            rgx = new Regex("(.*)#(.*)#(.*)#(.*)#(.*)#(.*)", RegexOptions.Compiled);
            StockItem item = new StockItem();

            int count = 0;

            while ((line = sr.ReadLine()) != null && line.Length > 0)
            {
                Match           match  = rgx.Match(line);
                GroupCollection groups = match.Groups;
                if (groups.Count < 7)
                {
                    throw new ApplicationException("Input error on inventory.txt");
                }
                InitStockItemFromRegex(item, groups);
                keyEntry  = dbs.Fmt.ToDbEntry <string>(item.Sku);
                dataEntry = dbs.Fmt.ToDbEntry <StockItem>(item);
                WriteStatus status = dbs.InventoryDb.Put(null, ref keyEntry, ref dataEntry);
                if (status != WriteStatus.Success)
                {
                    throw new Exception("Put failed");
                }
                count++;
            }
        }