slightly modified makefile. Added DB stuff

svn path=/trunk/gtk-sharp/; revision=6132
This commit is contained in:
Gonzalo Paniagua Javier 2002-07-24 13:46:19 +00:00
parent 6be8587936
commit d4a5d43707
2 changed files with 144 additions and 2 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Data; using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
@ -194,3 +195,139 @@ public class GtkDbClient {
Application.Quit (); Application.Quit ();
} }
} }
class DbData
{
private uint id;
private string name;
private string address;
private DbData () {}
public DbData (uint id, string name, string address)
{
this.id = id;
this.name = name;
this.address = address;
}
public uint Id
{
get { return id; }
}
public string Name
{
get { return name; }
}
public string Address
{
get { return address; }
}
}
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 (int id)
{
string deleteCmd = String.Format ("DELETE FROM customers WHERE id = {0}", id);
IDbCommand deleteCommand = cnc.CreateCommand();
deleteCommand.CommandText = deleteCmd;
deleteCommand.ExecuteNonQuery ();
}
public bool Update (int 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 DbData Select (int 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 (DbData) list [0];
}
private ArrayList FillDataList (IDataReader reader)
{
ArrayList list = new ArrayList ();
while (reader.Read ()) {
DbData data = new DbData ((uint) reader.GetValue (0),
(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);
}
}

View file

@ -1,7 +1,12 @@
MCS=mcs MCS=mcs
REFERENCES= glib-sharp gdk-sharp gtk-sharp gnome-sharp System.Data
###
REFS= $(addsuffix .dll, $(addprefix /r:, $(REFERENCES)))
all: all:
$(MCS) -r glib-sharp -r gdk-sharp -r gtk-sharp -r gnome-sharp DbClient.cs $(MCS) $(REFS) DbClient.cs
clean: clean:
rm -f *.exe rm -f *.exe *.pdb