MainFrm.cs :  » Database » Berkeley-DB » getting_started » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Database » Berkeley DB 
Berkeley DB » getting_started » MainFrm.cs
/*
 * This software is licensed according to the "Modified BSD License",
 * where the following substitutions are made in the license template:
 * <OWNER> = Karl Waclawek
 * <ORGANIZATION> = Karl Waclawek
 * <YEAR> = 2005, 2006
 * It can be obtained from http://opensource.org/licenses/bsd-license.html.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
using BerkeleyDb;
using Kds.Serialization.Buffer;

namespace getting_started{
  public partial class MainFrm: Form
  {
    public MainFrm() {
      InitializeComponent();
    }

    private FileStream errStream;
    private byte[] keyBuffer = new byte[1024];
    private byte[] dataBuffer = new byte[4096];

    private void InitVendorFromRegex(Vendor vendor, GroupCollection groups) {
      vendor.Name = groups[1].Value;
      vendor.Street = groups[2].Value;
      vendor.City = groups[3].Value;
      vendor.State = groups[4].Value;
      vendor.ZipCode = groups[5].Value;
      vendor.PhoneNumber = groups[6].Value;
      SalesRep rep = new SalesRep();
      rep.Name = groups[7].Value;
      rep.PhoneNumber = groups[8].Value;
      vendor.SalesRep = rep;
    }

    private void InitStockItemFromRegex(StockItem item, GroupCollection groups) {
      item.Name = groups[1].Value;
      item.Sku = groups[2].Value;
      item.Price = float.Parse(groups[3].Value);
      item.Quantity = int.Parse(groups[4].Value);
      item.Category = groups[5].Value;
      item.Vendor = groups[6].Value;
    }

    // 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++;
      }
    }

    private List<Vendor> ReadVendors() {
      Databases dbs = Databases.Instance;
      List<Vendor> vendors = new List<Vendor>();
      // using a foreach loop (which closes the cursor automatically)
      foreach (KeyDataPair entry in dbs.VendorDb.OpenCursor(null, DbFileCursor.CreateFlags.None)) {
        Vendor vendor = null;
        dbs.Fmt.FromDbEntry<Vendor>(ref vendor, ref entry.Data);
        vendors.Add(vendor);
      }

      vendorView.DataSource = null;
      DataGridViewColumn col = new DataGridViewTextBoxColumn();
      col.Name = "NameCol";
      col.HeaderText = "Name";
      col.DataPropertyName = "Name";
      vendorView.Columns.Add(col);
      col = new DataGridViewTextBoxColumn();
      col.Name = "SalesRepCol";
      col.HeaderText = "Sales Rep";
      col.DataPropertyName = "SalesRep";
      vendorView.Columns.Add(col);
      vendorView.DataSource = vendors;
      return vendors;
    }

    private List<StockItem> ReadStockItems(Vendor vendor) {
      Databases dbs = Databases.Instance;
      List<StockItem> items = new List<StockItem>();
      DbBTreeCursor cursor = dbs.VendorNameIdx.OpenCursor(null, DbFileCursor.CreateFlags.None);
      try {
        DbEntry secKeyEntry = dbs.Fmt.ToDbEntry<string>(vendor.Name);

#if false
        // same as below, but using standard cursor methods
        DbEntry keyEntry = DbEntry.Out(keyBuffer);
        DbEntry dataEntry = DbEntry.Out(dataBuffer);
        ReadStatus status;
        status = cursor.PGetAt(ref secKeyEntry, ref keyEntry, ref dataEntry,
          DbcFile.GetAtMode.Set, DbcFile.ReadFlags.None);
        // we are not checking for ReadStatus.BufferSmall, but maybe we should
        while (status == ReadStatus.Success) {
          StockItem item = null;
          index = dataEntry.Start;
          dbs.Formatter.Deserialize<StockItem>(ref item, dataEntry.Buffer, ref index);
          items.Add(item);
          status = cursor.PGet(ref secKeyEntry, ref keyEntry, ref dataEntry,
             DbcFile.GetMode.NextDup, DbcFile.ReadFlags.None);
        }
#endif
#if true
        // same as above, but using a foreach loop (which closes the cursor automatically)
        foreach (KeyDataPair entry in cursor.ItemsAt(ref secKeyEntry, DbFileCursor.ReadFlags.None)) {
          StockItem item = null;
          dbs.Fmt.FromDbEntry<StockItem>(ref item, ref entry.Data);
          items.Add(item);
        }
#endif

      }
      finally {
        cursor.Close();
      }

      inventoryView.DataSource = null;
      DataGridViewColumn col = new DataGridViewTextBoxColumn();
      col.Name = "NameCol";
      col.HeaderText = "Name";
      col.DataPropertyName = "Name";
      inventoryView.Columns.Add(col);
      inventoryView.DataSource = items;
      return items;
    }

    private void readBtn_Click(object sender, EventArgs e) {
      if (Databases.Instance.VendorDb == null)
        throw new ApplicationException("Databases closed.");
      List<Vendor> vendors = ReadVendors();
    }

    private void MainFrm_FormClosed(object sender, FormClosedEventArgs e) {
      Databases.Instance.Close();
    }

    private void vendorView_RowEnter(object sender, DataGridViewCellEventArgs e) {
      if (Databases.Instance.VendorDb == null)
        return;
      Vendor vendor = ((List<Vendor>)vendorView.DataSource)[e.RowIndex];
      ReadStockItems(vendor);
    }

    private void vendorView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
      if (vendorView.Rows.Count <= e.RowIndex)
        return;
      Vendor vendor = (Vendor)vendorView.Rows[e.RowIndex].DataBoundItem;
      if (vendor == null)
        return;
      if (vendorView.Columns[e.ColumnIndex].Name == "SalesRepCol")
        e.Value = vendor.SalesRep.Name + " (" + vendor.SalesRep.PhoneNumber + ")";
      e.FormattingApplied = true;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.