[Mono-devel-list] MySQL + C#
Mike Morano
mmorano at mikeandwan.us
Sun Jun 20 09:46:09 EDT 2004
Hello,
I think this is better suited in the mono user list, so for questions like
this in the future, try there first.
Now to answer your question. Try changing the loop to the following, it looks
like the error is in how the reader is being accessed. As you can see in the
following code, there are special methods on the IDataReader, to pull out
values of a particular type (in this example only strings). Therefore if you
have different types in the db, you will want to change the method you use to
get the data from the reader:
while(reader.Read())
{
string name = reader.GetString(reader.GetOrdinal("pkg_name"));
string price = reader.GetString(reader.GetOrdinal("pkg_baseprice"));
Console.Out.WriteLine("Name: " + name + " " + price);
}
Additionally, for the projects you port, you will want to make your app a bit
more robust than this example, as far as dealing with exceptions, and
ensuring your app doesn't leave connections lying around. Therfore do
something like this, so you can be sure the db resorces are freed:
try
{
// do your db access here
}
catch
{
// log / show error message
}
finally
{
// close all db related resources
}
Good luck,
Mike
On Saturday 19 June 2004 04:42 pm, Colt D. Majkrzak wrote:
> Hi, I'm fairly new to c# and the mono project so forgive me if this
> question is rather trivial.
>
>
>
> I'm trying to port some of my Linux programs into c#, well 90% of them
> require MySQL to function. I found the example code on the mono site for
> making a client connection, but I'm receiving the following error on
> execution (happens on Linux (RH9) or my XP machine w/ Mono installed)
>
>
>
> C:\>mono test.exe
>
>
>
> Unhandled Exception: System.InvalidCastException: Cannot cast from source
> type t
>
> o destination type.
>
> in <0x00124> Test:Main ()
>
>
>
> [spider at serv1 test]$ mono test.exe
>
>
>
> Unhandled Exception: System.InvalidCastException: Cannot cast from source
> type to destination type.
>
> in <0x0013a> Test:Main ()
>
>
>
> -----Code---
>
> using System;
>
> using System.Data;
>
> using ByteFX.Data.MySqlClient;
>
>
>
> public class Test
>
> {
>
> public static void Main()
>
> {
>
> string connectionString =
>
> "Server=localhost;" +
>
> "Database=database;" +
>
> "User ID=username;" +
>
> "Password=password;";
>
> IDbConnection dbcon;
>
> dbcon = new MySqlConnection(connectionString);
>
> dbcon.Open();
>
> IDbCommand dbcmd = dbcon.CreateCommand();
>
> // requires a table to be created named employee
>
> // with columns firstname and lastname
>
> // such as,
>
> // CREATE TABLE employee (
>
> // firstname varchar(32),
>
> // lastname varchar(32));
>
> string sql =
>
> "SELECT pkg_name, pkg_baseprice " +
>
> "FROM packages";
>
> dbcmd.CommandText = sql;
>
> IDataReader reader = dbcmd.ExecuteReader();
>
> while(reader.Read()) {
>
> string PkgName = (string) reader["pkg_name"];
>
> string Price = (string) reader["pkg_baseprice"];
>
> Console.WriteLine("Name: " +
>
> PkgName + " " + Price);
>
> }
>
> // clean up
>
> reader.Close();
>
> reader = null;
>
> dbcmd.Dispose();
>
> dbcmd = null;
>
> dbcon.Close();
>
> dbcon = null;
>
> }
>
> }
>
>
>
>
>
> Thank in advance.
More information about the Mono-devel-list
mailing list