September 10, 2008

Mono's interacive C# and using System.Data

I wanted to try out the new tool called csharp which allows you to interfactively enter commmands in C# and it compiles and run them. In my case, I wanted to see it connecting to a database. Here is my results - typos and all:

danmorg@danspc:~/monosvn/monoinstall/etc/mono> csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> LoadAssembly("System.Data.dll");
csharp> using System.Data;
csharp> using System.Data.Common;
csharp> DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OracleClient");
csharp> IDbConnection con = (IDbConnection) factory.CreateConnection();
csharp> con.ConnectionString = "server=testdb;user id=scott;password=tiger";
"server=testdb;user id=scott;password=tiger"
csharp> con.Open();
csharp> con.State;
csharp> IDbCommand cmd = con.CreateCommand();
csharp> cmd.CommadText = "select 'one' as num from dual union select 'two' from dual union select 'three' from dual";
(1,6): error CS1061: Type `System.Data.IDbCommand' does not contain a definition for `CommadText' and no extension method `CommadText' of type `System.Data.IDbCommand' could be found (are you missing a using directive or an assembly reference?)
csharp> cmd.CommandText = "select 'one' as num from dual union select 'two' from"select 'one' as num from dual union select 'two' from dual union select 'three' from dual"
csharp> IDataReader reader = cmd.ExecuteReader();
csharp> whlie (reader.Read())
> Console.WriteLine(reader.GetString(0));
(1,2): error CS1002: Expecting `;'
(2,8): error CS1002: Expecting `[', `)', `,', `;', or `=', got .
csharp> while (reader.Read())
> Console.WriteLine(reader.GetString(0));
csharp> reader.Close();
csharp> con.Close();
csharp> con.State;

The errors I got usually were due to typos. I didn't have to re-type everything, just clicked the up arrow to get a command in my history.

Also, here are two important commands to remember:

To get help, type: help;

To quit, type:

Posted by danmorg at September 10, 2008 10:30 PM