Hi all,
I've been browsing the forum and have googled for this problem, but I can't seem to fix it. I keep getting an
'cannot open connection' exception and the inner exception gives a
'Could not create connection from Driver' exception.
I am using .Net 2.0, created a Console application for testing purposes, the connectionstring I use works when I am not using NHibernate.
Any suggestions?
This is what I do:
- I copied the MySql.Data.dll to the bin\Debug folder
- I reference ICSharpCode.SharpZipLib and NHibernate from my project (and they also appear in the bin\Debug folder)
The class I created:
Code:
using System;
namespace NHibernatePrototype
{
public class Gig
{
private int _id;
private string _locationName;
private string _locationUrl;
private DateTime _date;
private DateTime _start;
private DateTime _end;
public Gig()
{}
public Gig(int id)
{
_id = id;
}
public virtual int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public virtual string LocationName
{
get
{
return _locationName;
}
set
{
_locationName = value;
}
}
public virtual string LocationUrl
{
get
{
return _locationUrl;
}
set
{
_locationUrl = value;
}
}
public virtual DateTime Date
{
get
{
return _date;
}
set
{
_date = value;
}
}
public virtual DateTime Start
{
get
{
return _start;
}
set
{
_start = value;
}
}
public virtual DateTime End
{
get
{
return _end;
}
set
{
_end = value;
}
}
}
}
The Gig.hbm.xml file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernatePrototype.Gig, NHibernatePrototype" table="gigs">
<id name="Id" column="id" type="int">
<generator class="assigned" />
</id>
<property name="LocationName" column="location-name" type="String" length="40" />
<property name="LocationUrl" column="location-url" type="String" length="20" />
<property name="Date" column="date" type="DateTime" />
<property name="Start" column="start" type="DateTime" />
<property name="End" column="end" type="DateTime" />
</class>
</hibernate-mapping>
The App.config:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="nhibernate"
type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</configSections>
<nhibernate>
<add key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect"
value="NHibernate.Dialect.MySQLDialect" />
<add key="hibernate.connection.driver_class"
value="NHibernate.Driver.MySqlDataDriver" />
<add key="hibernate.connection.connection_string"
value="server=localhost; user id=prototype; password=prototype; database=prototype; pooling=false;" />
</nhibernate>
</configuration>
And finally the main application that has the client code:
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using NHibernate;
using NHibernate.Cfg;
namespace NHibernatePrototype
{
public class Program
{
static void Main(string[] args)
{
try
{
Configuration cfg = new Configuration();
cfg.AddAssembly("NHibernatePrototype");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
IList gigList = session.CreateCriteria(typeof(Gig)).List();
foreach (Gig gig in gigList)
{
Console.WriteLine(gig.LocationName);
}
session.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
Console.ReadLine();
}
}
}
Any idea why I am not able to establish a connection to the database?[/code]