<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Th30z - Coding on the Fly &#187; C#</title>
	<atom:link href="http://th30z.netsons.org/tag/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://th30z.netsons.org</link>
	<description>Matteo Bertozzi, Objective-C, Cocoa, C, C++, Qt4, iPhone, Mac OS X, Open Moko, Matteo Bertozzi Development</description>
	<lastBuildDate>Sun, 22 Nov 2009 09:47:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C# Oracle for Beginners</title>
		<link>http://th30z.netsons.org/2009/04/cs-oracle-for-beginners/</link>
		<comments>http://th30z.netsons.org/2009/04/cs-oracle-for-beginners/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 05:22:26 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=774</guid>
		<description><![CDATA[In the last two days, at work I had a couple of hours to play with Oracle.
I&#8217;ve made a simple class to Wrap the .NET OracleConnection and here there&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>In the last two days, at work I had a couple of hours to play with Oracle.<br />
I&#8217;ve made a simple class to Wrap the .NET OracleConnection and here there&#8217;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.<br />
<code></p>
<pre>
OracleDatabase db = new OracleDatabase();
db.Connect(&quot;192.168.3.2&quot;, &quot;system&quot;, &quot;password&quot;);

// Create And Populate Tables
db.ExecuteNonQuery(&quot;CREATE TABLE tb1 (a NUMBER NOT NULL, b CHAR(20))&quot;);
db.ExecuteNonQuery(&quot;INSERT INTO tb1 (a, b) VALUES (10, 'Matteo')&quot;);
db.ExecuteNonQuery(&quot;INSERT INTO tb1 (a, b) VALUES (20, 'Mauro')&quot;);

db.ExecuteNonQuery(&quot;CREATE TABLE tb2 (a NUMBER NOT NULL, b CHAR(20))&quot;);
db.ExecuteNonQuery(&quot;INSERT INTO tb2 (a, b) VALUES (10, 'San Francisco')&quot;);
db.ExecuteNonQuery(&quot;INSERT INTO tb2 (a, b) VALUES (20, 'Cuppertino')&quot;);

// A Store Procedure cannot Return Value
StringBuilder sp1 = new StringBuilder();
sp1.Append(&quot;CREATE OR REPLACE PROCEDURE tb2sp1 &quot;);
sp1.Append(&quot;IS &quot;);
sp1.Append(&quot;BEGIN &quot;);
sp1.Append(&quot;INSERT INTO tb1 (a, b) VALUES (30, 'SP1 TEST'); &quot;);
sp1.Append(&quot;END; &quot;);
db.ExecuteNonQuery(sp1.ToString());

// A Function can Return a Value
StringBuilder sp2 = new StringBuilder();
sp2.Append(&quot;CREATE OR REPLACE function tb2sp2 (idx NUMBER) &quot;);
sp2.Append(&quot; RETURN NUMBER &quot;);
sp2.Append(&quot;IS &quot;);
sp2.Append(&quot; var_count NUMBER; &quot;);
sp2.Append(&quot;BEGIN &quot;);
sp2.Append(&quot; SELECT COUNT(*) INTO var_count FROM tb1; &quot;);
sp2.Append(&quot; return (var_count + idx); &quot;);
sp2.Append(&quot;END; &quot;);
db.ExecuteNonQuery(sp2.ToString());

List&lt;OracleDatabase.DataRecord&gt; r1 = db.Execute(&quot;select * from tb1&quot;);
List&lt;OracleDatabase.DataRecord&gt; r2 = db.Execute(&quot;select a, b, (a * 2 + 1) from tb1&quot;);
List&lt;OracleDatabase.DataRecord&gt; r3 = db.Execute(&quot;select tb1.a as ID, tb1.b as NAME,
                              tb2.b as PLACE from tb1 INNER JOIN tb2 ON tb1.a = tb2.a&quot;);
db.ExecuteStoredProcedure(&quot;tb2sp1&quot;);
List&lt;OracleDatabase.DataRecord&gt; r4 = db.Execute(&quot;select * from tb1&quot;);
object sp2RetVal = db.ExecuteFunction(&quot;tb2sp2(10)&quot;);

db.ExecuteNonQuery(&quot;DROP PROCEDURE tb2sp1&quot;);
db.ExecuteNonQuery(&quot;DROP FUNCTION tb2sp2&quot;);
db.ExecuteNonQuery(&quot;DROP TABLE tb2&quot;);
db.ExecuteNonQuery(&quot;DROP TABLE tb1&quot;);

db.Disconnect();
</pre>
<p></code><br />
Here you can find the Source Code: <a href="http://th30z.netsons.org/wp-content/uploads/cs-oracletests.zip">Oracle for Beginners Source Code</a>.</p>
<p><span id="more-774"></span><br />
The OracleDatabase class that you&#8217;ve seen in the example above, is a simple wrapper class, that you can see below.<br />
As always, we&#8217;ve two methods for Connect and Disconnect from Database Server.<br />
<code></p>
<pre>
public bool Connect(string sid, string userId, string password) {
    string cs = String.Format(&quot;Data Source={0}; User Id={1};
	                           Password={2};&quot;, sid, userId, password);
    _oracle = new OracleConnection(cs);
    _oracle.Open();
    return (_oracle.State == ConnectionState.Open);
}

public void Disconnect() {
    if (_oracle != null &amp;&amp; _oracle.State != ConnectionState.Closed)
        _oracle.Close();
}
</pre>
<p></code><br />
Below there&#8217;re the methods that helps you to retrieve SELECT data, and to run non query like INSERT, UPDATE, DELETE, DROP&#8230;<br />
<code></p>
<pre>
public List&lt;DataRecord&gt; Execute(string query, params object[] args) {
    using (OracleCommand cmd = new OracleCommand(String.Format(query, args), _oracle)) {
        cmd.CommandType = CommandType.Text;
        return (Execute(cmd));
    }
}

private List&lt;DataRecord&gt; Execute(OracleCommand cmd) {
    List&lt;DataRecord&gt; dataRecordList = new List&lt;DataRecord&gt;();
    using (OracleDataReader reader = cmd.ExecuteReader()) {
        while (reader.Read()) {
            DataRecord record = new DataRecord();
            for (int i = 0; i &lt; reader.FieldCount; ++i)
                record.Add(reader.GetName(i), reader[i]);
            dataRecordList.Add(record);
        }
        reader.Close();
    }
    return (dataRecordList);
}

public int ExecuteNonQuery(string query, params object[] args) {
    using (OracleCommand cmd = new OracleCommand(String.Format(query, args), _oracle)) {
        cmd.CommandType = CommandType.Text;
        return(cmd.ExecuteNonQuery());
    }
}
</pre>
<p></code><br />
Below two methods to Execute a Function and a Stored Procedure, functions can have a return value.<br />
<code></p>
<pre>
public object ExecuteFunction(string query, params object[] args) {
    using (OracleCommand cmd = new OracleCommand(String.Format(query, args), _oracle)) {
        cmd.CommandType = CommandType.StoredProcedure;
        OracleParameter retParameter = new OracleParameter();
        retParameter.Direction = ParameterDirection.ReturnValue;
        retParameter.OracleType = OracleType.Number;
        cmd.Parameters.Add(retParameter);
        cmd.ExecuteNonQuery();
        return (retParameter.Value);
    }
}

public int ExecuteStoredProcedure(string query, params object[] args) {
    using (OracleCommand cmd = new OracleCommand(String.Format(query, args), _oracle)) {
        cmd.CommandType = CommandType.StoredProcedure;
        return (cmd.ExecuteNonQuery());
    }
}
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2009/04/cs-oracle-for-beginners/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# Parallel Foreach using Thread Pool</title>
		<link>http://th30z.netsons.org/2008/12/c-parallel-foreach-using-thread-pool/</link>
		<comments>http://th30z.netsons.org/2008/12/c-parallel-foreach-using-thread-pool/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 12:56:56 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=548</guid>
		<description><![CDATA[
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; [...]]]></description>
			<content:encoded><![CDATA[<p><code></p>
<pre>
public void Foreach(T[] array, Action<T> 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();
   }
}
</pre>
<p></code></p>
<p><code></p>
<pre>
public void ForeachWithResults<T1>(T[] array, T1[] results, Action<int, T, T1[]> 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();
   }
}
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2008/12/c-parallel-foreach-using-thread-pool/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C#: Evaluating Infix Expression</title>
		<link>http://th30z.netsons.org/2008/12/cs-evaluating-infix-expression/</link>
		<comments>http://th30z.netsons.org/2008/12/cs-evaluating-infix-expression/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 18:26:04 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Expression Evaluator]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=452</guid>
		<description><![CDATA[This morning I&#8217;ve written a simple Infix Notation Evaluator in C, and this afternoon I&#8217;ve rewritten it in C#. I Think that C# is more easy to understand than C, so I&#8217;ve posted the C# Example. (If you want the C example, ask me).
In few words, I take the Input in &#8220;Infix Notation&#8221; and I [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I&#8217;ve written a simple <a href="http://en.wikipedia.org/wiki/Infix_notation">Infix Notation</a> Evaluator in C, and this afternoon I&#8217;ve rewritten it in C#. I Think that C# is more easy to understand than C, so I&#8217;ve posted the C# Example. (If you want the C example, ask me).<br />
In few words, I take the Input in &#8220;Infix Notation&#8221; and I convert it in <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation</a> (RPN), and then the RPN string is evaluated.</p>
<p>First Example: Evaluating a simple Math Expression.</p>
<pre>
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());
</pre>
<p>Second Example: Evaluating a simple Math Expression with variables.</p>
<pre>
Dictionary<string, string> vars = new Dictionary<string, string>();
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());
</pre>
<p>Third example: Evaluating simple String expression.</p>
<pre>
string expr = "'Hello' + ' ' * 3 + \"World\"";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
</pre>
<p>After the Example you can find the <a href="http://th30z.netsons.org/wp-content/uploads/tokenerevaluator.cs">Source Code</a> Here (You can compile it under Linux with gmcs).</p>
<p>That's all folks! I'm really busy with the Real Work, I'm waiting the 25th for taking a break.</p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2008/12/cs-evaluating-infix-expression/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Qyoto &#8211; First Experiment</title>
		<link>http://th30z.netsons.org/2008/07/qyoto-first-experiment/</link>
		<comments>http://th30z.netsons.org/2008/07/qyoto-first-experiment/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 13:02:12 +0000</pubDate>
		<dc:creator>Matteo Bertozzi</dc:creator>
				<category><![CDATA[Qt4]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Qyoto]]></category>

		<guid isPermaLink="false">http://th30z.netsons.org/?p=73</guid>
		<description><![CDATA[A little example of Mono, C# and Qt4 (Qyoto).
This examples contains all what you need to starting a little application. QtDesigner, Resources, and Slot handling.
If you&#8217;re using Ubuntu, you must install libqyoto4.3-cli and qyoto-dev-tools.
Qt User Interface Compiler (uic) for C# is called&#160;uics and (in ubuntu) you can find it here:&#160;/usr/lib/kde4/bin/
Qt Resource Compiler (rcc) for C# [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://th30z.netsons.org/wp-content/uploads/qyototest.png"><img class="alignright size-medium wp-image-74" title="Qyoto Test Window" src="http://th30z.netsons.org/wp-content/uploads/qyototest-286x350.png" alt="" width="286" height="350" /></a>A little example of Mono, C# and Qt4 (<a title="Qyoto" href="http://www.qyoto.org/">Qyoto</a>).<br />
This examples contains all what you need to starting a little application. QtDesigner, Resources, and Slot handling.</p>
<p>If you&#8217;re using Ubuntu, you must install <strong>libqyoto4.3-cli</strong> and <strong>qyoto-dev-tools</strong>.</p>
<p><em>Qt User Interface Compiler</em> (uic) for C# is called&nbsp;<em>uics</em> and (in ubuntu) you can find it here:&nbsp;/usr/lib/kde4/bin/</p>
<p><em>Qt Resource Compiler</em> (rcc) for C# is called&nbsp;<em>csrcc</em> and is in the same path of <em>uics</em>.</p>
<p>To define Slots you should add attribute&nbsp;[Q_SLOT("slotName()")] on slot Method.</p>
<p>Another thing to&nbsp;remember&nbsp;is that all methods start with Upper case, so you&#8217;ve to call textEdit.SetText(&#8221;myText&#8221;) and not textEdit.setText(&#8221;myText&#8221;) like in C++.</p>
<p>Source Code of the example could be downloaded from here:&nbsp;<a href="http://th30z.netsons.org/wp-content/uploads/qyototest.tar.bz2">QyotoTest</a></p>
]]></content:encoded>
			<wfw:commentRss>http://th30z.netsons.org/2008/07/qyoto-first-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
