April 25, 2009 | Filed Under C# | 1 Comment
In the last two days, at work I had a couple of hours to play with Oracle.
I’ve made a simple class to Wrap the .NET OracleConnection and here there’s a simple example with Connection/Disconnection a couple of CREATE TABLE, INSERT a STORED PROCEDURE and a FUNCTION a couple of SELECT to show JOIN and other operations.
OracleDatabase db = new OracleDatabase();
db.Connect("192.168.3.2", "system", "password");
// Create And Populate Tables
db.ExecuteNonQuery("CREATE TABLE tb1 (a NUMBER NOT NULL, b CHAR(20))");
db.ExecuteNonQuery("INSERT INTO tb1 (a, b) VALUES (10, 'Matteo')");
db.ExecuteNonQuery("INSERT INTO tb1 (a, b) VALUES (20, 'Mauro')");
db.ExecuteNonQuery("CREATE TABLE tb2 (a NUMBER NOT NULL, b CHAR(20))");
db.ExecuteNonQuery("INSERT INTO tb2 (a, b) VALUES (10, 'San Francisco')");
db.ExecuteNonQuery("INSERT INTO tb2 (a, b) VALUES (20, 'Cuppertino')");
// A Store Procedure cannot Return Value
StringBuilder sp1 = new StringBuilder();
sp1.Append("CREATE OR REPLACE PROCEDURE tb2sp1 ");
sp1.Append("IS ");
sp1.Append("BEGIN ");
sp1.Append("INSERT INTO tb1 (a, b) VALUES (30, 'SP1 TEST'); ");
sp1.Append("END; ");
db.ExecuteNonQuery(sp1.ToString());
// A Function can Return a Value
StringBuilder sp2 = new StringBuilder();
sp2.Append("CREATE OR REPLACE function tb2sp2 (idx NUMBER) ");
sp2.Append(" RETURN NUMBER ");
sp2.Append("IS ");
sp2.Append(" var_count NUMBER; ");
sp2.Append("BEGIN ");
sp2.Append(" SELECT COUNT(*) INTO var_count FROM tb1; ");
sp2.Append(" return (var_count + idx); ");
sp2.Append("END; ");
db.ExecuteNonQuery(sp2.ToString());
List<OracleDatabase.DataRecord> r1 = db.Execute("select * from tb1");
List<OracleDatabase.DataRecord> r2 = db.Execute("select a, b, (a * 2 + 1) from tb1");
List<OracleDatabase.DataRecord> r3 = db.Execute("select tb1.a as ID, tb1.b as NAME,
tb2.b as PLACE from tb1 INNER JOIN tb2 ON tb1.a = tb2.a");
db.ExecuteStoredProcedure("tb2sp1");
List<OracleDatabase.DataRecord> r4 = db.Execute("select * from tb1");
object sp2RetVal = db.ExecuteFunction("tb2sp2(10)");
db.ExecuteNonQuery("DROP PROCEDURE tb2sp1");
db.ExecuteNonQuery("DROP FUNCTION tb2sp2");
db.ExecuteNonQuery("DROP TABLE tb2");
db.ExecuteNonQuery("DROP TABLE tb1");
db.Disconnect();
Here you can find the Source Code: Oracle for Beginners Source Code.
Continue reading C# Oracle for Beginners…
December 26, 2008 | Filed Under C# | 1 Comment
public void Foreach(T[] array, Action action)
{
int cpus = 2 * Environment.ProcessorCount;
int nitems = array.Length;
int chunk = nitems / cpus;
int counter = cpus;
using (AutoResetEvent signal = new AutoResetEvent(false))
{
for (int i = 1; i <= cpus; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
int unit = (int)o;
for (int j = (unit - 1) * chunk;
j < (unit == cpus ? nitems : unit * chunk);
j++)
{
action(array[j]);
}
if (Interlocked.Decrement(ref counter) == 0)
signal.Set();
}, i);
}
signal.WaitOne();
}
}
public void ForeachWithResults(T[] array, T1[] results, Action action)
{
int cpus = 2 * Environment.ProcessorCount;
int nitems = array.Length;
int chunk = nitems / cpus;
int counter = cpus;
using (AutoResetEvent signal = new AutoResetEvent(false))
{
for (int i = 1; i <= cpus; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
int unit = (int)o;
for (int j = (unit - 1) * chunk;
j < (unit == cpus ? nitems : unit * chunk);
j++)
{
action(j, array[j], results);
}
if (Interlocked.Decrement(ref counter) == 0)
signal.Set();
}, i);
}
signal.WaitOne();
}
}
December 20, 2008 | Filed Under C# | 1 Comment
This morning I’ve written a simple Infix Notation Evaluator in C, and this afternoon I’ve rewritten it in C#. I Think that C# is more easy to understand than C, so I’ve posted the C# Example. (If you want the C example, ask me).
In few words, I take the Input in “Infix Notation” and I convert it in Reverse Polish Notation (RPN), and then the RPN string is evaluated.
First Example: Evaluating a simple Math Expression.
string expr = "10 + 5 - (2 + (3 * 4) + (5 * 3)) - 4 ^ 2 << 8";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
Second Example: Evaluating a simple Math Expression with variables.
Dictionary vars = new Dictionary();
vars.Add("A", "1"); vars.Add("B", "2");
vars.Add("C", "3"); vars.Add("D", "4");
vars.Add("K", "5"); vars.Add("L", "6");
string expr = "(A + B) + C + (D * (K + (L - 1)))";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
Third example: Evaluating simple String expression.
string expr = "'Hello' + ' ' * 3 + \"World\"";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
After the Example you can find the Source Code Here (You can compile it under Linux with gmcs).
That's all folks! I'm really busy with the Real Work, I'm waiting the 25th for taking a break.