Insert and Delete now work
svn path=/trunk/gtk-sharp/; revision=6144
This commit is contained in:
parent
dd6a2b8878
commit
6a7c95725c
1 changed files with 152 additions and 27 deletions
|
@ -1,5 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
using Gtk;
|
using Gtk;
|
||||||
using GtkSharp;
|
using GtkSharp;
|
||||||
|
|
||||||
|
@ -12,6 +16,8 @@ class Client {
|
||||||
static Entry id_entry = null;
|
static Entry id_entry = null;
|
||||||
static Entry name_entry = null;
|
static Entry name_entry = null;
|
||||||
static Entry address_entry = null;
|
static Entry address_entry = null;
|
||||||
|
static VBox box = null;
|
||||||
|
static IdConnection conn = null;
|
||||||
|
|
||||||
static void Main ()
|
static void Main ()
|
||||||
{
|
{
|
||||||
|
@ -20,15 +26,14 @@ class Client {
|
||||||
window.DeleteEvent += new DeleteEventHandler (Window_Delete);
|
window.DeleteEvent += new DeleteEventHandler (Window_Delete);
|
||||||
window.DefaultSize = new Size (300, 200);
|
window.DefaultSize = new Size (300, 200);
|
||||||
|
|
||||||
VBox box = new VBox (false, 0);
|
box = new VBox (false, 0);
|
||||||
window.Add (box);
|
window.Add (box);
|
||||||
|
|
||||||
toolbar = new Toolbar ();
|
toolbar = new Toolbar ();
|
||||||
PackToolbar ();
|
PackToolbar ();
|
||||||
box.PackStart (toolbar, false, false, 0);
|
box.PackStart (toolbar, false, false, 0);
|
||||||
|
|
||||||
tableau = CreateView ();
|
UpdateView ();
|
||||||
box.PackStart (tableau, false, false, 0);
|
|
||||||
|
|
||||||
window.ShowAll ();
|
window.ShowAll ();
|
||||||
Application.Run ();
|
Application.Run ();
|
||||||
|
@ -50,7 +55,7 @@ class Client {
|
||||||
|
|
||||||
toolbar.AppendItem ("Refresh", "Refresh the view", String.Empty,
|
toolbar.AppendItem ("Refresh", "Refresh the view", String.Empty,
|
||||||
new Gtk.Image (Stock.Refresh, IconSize.LargeToolbar),
|
new Gtk.Image (Stock.Refresh, IconSize.LargeToolbar),
|
||||||
new SignalFunc (Db_Update), IntPtr.Zero);
|
new SignalFunc (UpdateView), IntPtr.Zero);
|
||||||
|
|
||||||
toolbar.AppendSpace ();
|
toolbar.AppendSpace ();
|
||||||
|
|
||||||
|
@ -66,16 +71,30 @@ class Client {
|
||||||
args.RetVal = true;
|
args.RetVal = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Gtk.Table CreateView ()
|
static void UpdateView ()
|
||||||
{
|
{
|
||||||
Table t = new Gtk.Table (0, 2, true);
|
if (tableau != null)
|
||||||
DrawTitles (t);
|
tableau.Destroy ();
|
||||||
t.ColSpacings = 10;
|
|
||||||
|
|
||||||
return t;
|
ArrayList dataList = Conn.SelectAll ();
|
||||||
|
|
||||||
|
tableau = new Gtk.Table ((uint) dataList.Count + 1, 3, true);
|
||||||
|
DrawTitles (tableau);
|
||||||
|
tableau.ColSpacings = 10;
|
||||||
|
uint i = 1;
|
||||||
|
foreach (Record r in dataList) {
|
||||||
|
tableau.Attach (new Label (r.ID.ToString ()), 0, 1, i, i + 1);
|
||||||
|
tableau.Attach (new Label (r.Name), 1, 2, i, i + 1);
|
||||||
|
tableau.Attach (new Label (r.Address), 2, 3, i, i + 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
tableau.Show ();
|
||||||
|
box.PackStart (tableau, false, false, 0);
|
||||||
|
box.ShowAll ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DrawTitles (Table t)
|
static void DrawTitles (Gtk.Table t)
|
||||||
{
|
{
|
||||||
Label label = null;
|
Label label = null;
|
||||||
|
|
||||||
|
@ -96,11 +115,6 @@ class Client {
|
||||||
t.Attach (label, 0, 3, 0, 1, AttachOptions.Expand, AttachOptions.Expand, 1, 1);
|
t.Attach (label, 0, 3, 0, 1, AttachOptions.Expand, AttachOptions.Expand, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void UpdateView (Record [] records)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Db_Insert ()
|
static void Db_Insert ()
|
||||||
{
|
{
|
||||||
dialog = new Dialog ();
|
dialog = new Dialog ();
|
||||||
|
@ -123,6 +137,7 @@ class Client {
|
||||||
button = Button.NewFromStock (Stock.Cancel);
|
button = Button.NewFromStock (Stock.Cancel);
|
||||||
button.Clicked += new EventHandler (Dialog_Cancel);
|
button.Clicked += new EventHandler (Dialog_Cancel);
|
||||||
dialog.ActionArea.PackStart (button, true, true, 0);
|
dialog.ActionArea.PackStart (button, true, true, 0);
|
||||||
|
dialog.Modal = true;
|
||||||
|
|
||||||
dialog.ShowAll ();
|
dialog.ShowAll ();
|
||||||
}
|
}
|
||||||
|
@ -188,11 +203,6 @@ class Client {
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void Db_Update ()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Quit ()
|
static void Quit ()
|
||||||
{
|
{
|
||||||
Application.Quit ();
|
Application.Quit ();
|
||||||
|
@ -200,14 +210,16 @@ class Client {
|
||||||
|
|
||||||
static void Insert_Action (object o, EventArgs args)
|
static void Insert_Action (object o, EventArgs args)
|
||||||
{
|
{
|
||||||
Console.WriteLine (String.Format ("ID: {0}\nName: {1}\nAddress: {2}",
|
Conn.Insert (UInt32.Parse (id_entry.Text), name_entry.Text, address_entry.Text);
|
||||||
id_entry.Text, name_entry.Text, address_entry.Text));
|
UpdateView ();
|
||||||
|
dialog.Destroy ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Remove_Action (object o, EventArgs args)
|
static void Remove_Action (object o, EventArgs args)
|
||||||
{
|
{
|
||||||
Console.WriteLine (String.Format ("ID: {0}\nName: {1}\nAddress: {2}",
|
Conn.Delete (UInt32.Parse (id_entry.Text));
|
||||||
id_entry.Text, name_entry.Text, address_entry.Text));
|
UpdateView ();
|
||||||
|
dialog.Destroy ();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Dialog_Cancel (object o, EventArgs args)
|
static void Dialog_Cancel (object o, EventArgs args)
|
||||||
|
@ -215,18 +227,131 @@ class Client {
|
||||||
dialog.Destroy ();
|
dialog.Destroy ();
|
||||||
dialog = null;
|
dialog = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static IdConnection Conn
|
||||||
|
{
|
||||||
|
get {
|
||||||
|
if (conn == null)
|
||||||
|
conn = new IdConnection ();
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Record {
|
struct Record {
|
||||||
|
public uint ID;
|
||||||
public int ID;
|
|
||||||
public string Name;
|
public string Name;
|
||||||
public string Address;
|
public string Address;
|
||||||
|
|
||||||
public Record (int i, string s, string t)
|
public Record (uint i, string s, string t)
|
||||||
{
|
{
|
||||||
ID = i;
|
ID = i;
|
||||||
Name = s;
|
Name = s;
|
||||||
Address = t;
|
Address = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class IdConnection : IDisposable
|
||||||
|
{
|
||||||
|
private SqlConnection cnc;
|
||||||
|
private bool disposed;
|
||||||
|
|
||||||
|
public IdConnection ()
|
||||||
|
{
|
||||||
|
cnc = new SqlConnection ();
|
||||||
|
string connectionString = "hostaddr=127.0.0.1;" +
|
||||||
|
"user=monotest;" +
|
||||||
|
"password=monotest;" +
|
||||||
|
"dbname=monotest";
|
||||||
|
|
||||||
|
cnc.ConnectionString = connectionString;
|
||||||
|
try {
|
||||||
|
cnc.Open ();
|
||||||
|
} catch (Exception){
|
||||||
|
cnc = null;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert (uint id, string name, string address)
|
||||||
|
{
|
||||||
|
string insertCmd = String.Format ("INSERT INTO customers VALUES ({0}, '{1}', '{2}')",
|
||||||
|
id, name.Trim (), address.Trim ());
|
||||||
|
IDbCommand insertCommand = cnc.CreateCommand();
|
||||||
|
insertCommand.CommandText = insertCmd;
|
||||||
|
insertCommand.ExecuteNonQuery ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete (uint id)
|
||||||
|
{
|
||||||
|
string deleteCmd = String.Format ("DELETE FROM customers WHERE id = {0}", id);
|
||||||
|
IDbCommand deleteCommand = cnc.CreateCommand();
|
||||||
|
deleteCommand.CommandText = deleteCmd;
|
||||||
|
deleteCommand.ExecuteNonQuery ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Update (uint id, string name, string address)
|
||||||
|
{
|
||||||
|
string updateCmd = String.Format ("UPDATE customers SET name = '{1}', address = '{2}' WHERE id = {0}",
|
||||||
|
id, name.Trim (), address.Trim ());
|
||||||
|
IDbCommand updateCommand = cnc.CreateCommand();
|
||||||
|
updateCommand.CommandText = updateCmd;
|
||||||
|
bool updated = false;
|
||||||
|
return (updateCommand.ExecuteNonQuery () != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList SelectAll ()
|
||||||
|
{
|
||||||
|
IDbCommand selectCommand = cnc.CreateCommand();
|
||||||
|
string selectCmd = "SELECT id, name, address FROM customers ORDER by id";
|
||||||
|
selectCommand.CommandText = selectCmd;
|
||||||
|
IDataReader reader = selectCommand.ExecuteReader ();
|
||||||
|
return FillDataList (reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Record Select (uint id)
|
||||||
|
{
|
||||||
|
IDbCommand selectCommand = cnc.CreateCommand();
|
||||||
|
string selectCmd = "SELECT id, name, address FROM customers WHERE id = " + id;
|
||||||
|
selectCommand.CommandText = selectCmd;
|
||||||
|
IDataReader reader = selectCommand.ExecuteReader ();
|
||||||
|
ArrayList list = FillDataList (reader);
|
||||||
|
return (Record) list [0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArrayList FillDataList (IDataReader reader)
|
||||||
|
{
|
||||||
|
ArrayList list = new ArrayList ();
|
||||||
|
while (reader.Read ()) {
|
||||||
|
Record data = new Record (UInt32.Parse (reader.GetValue (0).ToString ()),
|
||||||
|
(string) reader.GetValue (1),
|
||||||
|
(string) reader.GetValue (2));
|
||||||
|
list.Add (data);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose (bool exp)
|
||||||
|
{
|
||||||
|
if (!disposed && cnc != null) {
|
||||||
|
disposed = true;
|
||||||
|
try {
|
||||||
|
cnc.Close ();
|
||||||
|
} catch (Exception) {
|
||||||
|
}
|
||||||
|
cnc = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose ()
|
||||||
|
{
|
||||||
|
Dispose (true);
|
||||||
|
GC.SuppressFinalize (this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~IdConnection ()
|
||||||
|
{
|
||||||
|
Dispose (false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue