我用 nhibernate 做了一个简单的应用程序:class utilisateur.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="nhiber.utilisateur, nhiber" table="utilisateur">
<id name="login" type="string"></id>
<property name="password" type="string" />
<property name="age" type="int" />
<property name="location" type="string" />
<property name="name" type="string" />
</class>
</hibernate-mapping>
类utilisateur.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
namespace nhiber
{
public class utilisateur
{
public virtual string login { get; set; }
public virtual string password { get; set; }
public virtual string name { get; set; }
public virtual int age { get; set; }
public virtual string location { get; set; }
public virtual IList<utilisateur> get_liste_utilisateurs(){
using (ISession session = OpenSession())
{
IQuery query = session.CreateQuery("FROM utilisateur");
IList<utilisateur> pets = query.List<utilisateur>();
return pets;
}
}
public static ISessionFactory SessionFactory;
public static ISession OpenSession()
{
if (SessionFactory == null) //not threadsafe
{ Configuration config = new Configuration();
config.AddAssembly(Assembly.GetCallingAssembly());
SessionFactory = config.BuildSessionFactory();
}
return SessionFactory.OpenSession();
}
}
}
更详细的 App.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Simulation;Integrated Security=True;Pooling=False;
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<property name="show_sql">
false
</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="nhiber"/>
</session-factory>
</hibernate-configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
类form.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace nhiber
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
try
{
utilisateur u = new utilisateur();
for (int i = 0; i < u.get_liste_utilisateurs().Count; i++)
textBox1.Text += "\r\n" + u.get_liste_utilisateurs()[i].name;
}
catch (Exception e) { File.AppendAllText(@"C:\Users\HP\Desktop\test1.txt", e.ToString()); }
}
}
}
但我有这个例外:System.InvalidOperationException:Échec d'instance。à System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean IntegratedSecurity)à nhiber.utilisateur.OpenSession() dans C:\Users\HP\documents\ Visual Studio 2010\Projects\nhiber\utilisateur.cs:ligne 32 à nhiber.utilisateur.get_liste_utilisateurs() dans C:\Users\HP\documents\visual studio 2010\Projects\nhiber\utilisateur.cs:ligne 19 à nhiber.Form1 ..ctor() dans C:\Users\HP\documents\visual studio 2010\Projects\nhiber\Form1.cs:ligne 21
我需要帮助。有什么建议吗?