0

i do a simple application with 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>

class 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();
        }
    }
}

fichier 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>

class 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()); }
        }
    }
}

but i have this exception : 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

i need help.any advices?

1 Answer 1

1

I suspect this is the problem:

 Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Simulation (...)

My guess is that you copied this from a string literal, and that it should actually be

 Data Source=HP-PC\SQLEXPRESS;Initial Catalog=Simulation (...)

(Note the single backslash - which is the value you'd get from the double backslash in a plain literal in C# source code.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.