找不到命名连接字符串Server nhibernate MySql

时间:2014-11-18 06:27:02

标签: c# mysql nhibernate

编程和获取此错误的新手。 我不知道请帮助。 这是我的保存按钮点击和我的配置文件。

  private void save_Click(object sender, EventArgs e)
        {
            Details d =new Details();
            d.Firstname = firstname.Text;

            Configuration cfg=new Configuration();

            cfg.Configure("NHibernate.xml");
            ISessionFactory sessionfactory = cfg.BuildSessionFactory();
            ISession session = sessionfactory.OpenSession();
            ITransaction tx = session.BeginTransaction();
            session.Save(d);
            tx.Commit();
  }

我的配置文件Nhibernate.xml

<?xml version="1.0" encoding="utf-8" ?>

<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="dialect">
      NHibernate.Dialect.MySQLDialect,Nhibernate
    </property>
    <property name="connection.connection_string_name">
      Server=localhost;Database=mohit;User ID=root;Password=root
    </property>

    <mapping assembly="TestNhibernate"/>
  </session-factory>
  </hibernate-configuration>
</configuration>
please help

1 个答案:

答案 0 :(得分:0)

您可以使用 name="connection.connection_string"

<session-factory>
    ...
    <property name="connection.connection_string"> 
      Server=localhost;Database=mohit;User ID=root;Password=root
    </property>

name="connection.connection_string_name"配置<connectionStrings>

// another web.config section "<connectionStrings>"
<connectionStrings>
  <add name="MyName" 
    connectionString="Server=localhost;Database=mohit;User ID=root;Password=root" />
</connectionStrings>

// here we reference the name defined in the other section
<session-factory>
    ...
    <property name="connection.connection_string_name">MyName</property>