using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using com.db4o;
using com.db4o.query;
namespace db4otest
{ ///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{ ///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
string filepath = @"c:\temp\db4o.db";
public Form1()
{ //
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{ if( disposing )
{ if (components != null)
{ components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{ this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "AddRJ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(24, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 23);
this.button2.TabIndex = 1;
this.button2.Text = "ShowNumber";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(24, 88);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 23);
this.button3.TabIndex = 2;
this.button3.Text = "QueryForRJ";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(24, 112);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(104, 23);
this.button4.TabIndex = 3;
this.button4.Text = "Clear DB";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(24, 40);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(104, 23);
this.button5.TabIndex = 4;
this.button5.Text = "AddDennis";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(144, 149);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button5,
this.button4,
this.button3,
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{ Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{ ObjectContainer db = Db4o.openFile(filepath);
try
{ Account a = new Account("RJ","rj@rj.net",@"http://vanholland.net"); db.set(a);
}
finally
{ db.close();
}
}
private void button2_Click(object sender, System.EventArgs e)
{ ObjectContainer db = Db4o.openFile(filepath);
try
{ Account proto = new Account(null,null,null);
ObjectSet result = db.get(proto);
MessageBox.Show(result.size().ToString());
}
finally
{ db.close();
}
}
private void button4_Click(object sender, System.EventArgs e)
{ ObjectContainer db = Db4o.openFile(filepath);
ObjectSet result = db.get(new Account(null, null,null));
while (result.hasNext())
{ db.delete(result.next());
}
db.close();
}
private void button3_Click(object sender, System.EventArgs e)
{ ObjectContainer db = Db4o.openFile(filepath);
Query query = db.query();
query.constrain(typeof(Account));
query.descend("_name").constrain("RJ"); ObjectSet result = query.execute();
for (int i =0; i { if (result.hasNext())
{ Account a = (Account)result.next();
MessageBox.Show(a.Name);
}
else
{ break;
}
}
db.close();
}
private void button5_Click(object sender, System.EventArgs e)
{ ObjectContainer db = Db4o.openFile(filepath);
try
{ Account a = new Account("Dennis","Dennis@dennis.com",@"http://dennis.net"); db.set(a);
}
finally
{ db.close();
}
}
}
public class Account
{ string _name;
string _emailadres;
string _url;
public Account(string name, string emailadres, string url)
{ _name = name;
_emailadres = emailadres;
_url = url;
}
public string Name
{ get
{ return _name;
}
}
public string EmailAdres
{ get
{ return _emailadres;
}
}
public string URL
{ get
{ return _url;
}
}
}
}