Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘gt 5’

Add NHibernate Configuration in web.config, and reuse an existing connectionstring

When we use nHibernate in asp.net, most time we use a nhibernate.cfg.xml to define the database connection. And for some scenarioes, we may also have a database connection defined in web.config. So, there might be 2 definitions for the same database. Here is the way to combine the 2 definitions and in web.config.

in web.config

 1:  <configuration>
 2:    <configSections>
 3:      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
 4:    </configSections>
 5:    <appSettings>
 6:    </appSettings>
 7:    <connectionStrings>
 8:      <add name="MyDefaultDatabase" connectionString="Data Source=......" />
 9:    </connectionStrings>
10:    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
11:      <session-factory>
12:        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
13:        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
14:        <property name="connection.connection_string_name">MyDefaultDatabase</property>
15:        <property name="command_timeout">300</property>
16:        <property name="show_sql">false</property>
17:      </session-factory>
18:    </hibernate-configuration>
19:  ……………

 

and pay attention,

  1. The secion name must be ‘hibernate-configuration’ , or it will not work. Very tricky.
  2. The type must be ‘NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" , also very tricky.
  3. Use connection.connection_string_name if you want to reuse an existing database connection.
  4. in your code, use
  5. new NHibernate.Cfg.Configuration().Configure();
    instead of given a nhibernate.cfg.xml file parameter.

 

Now, you only have 1 database connections in your web.config~

C# Code Snippet: Finding a Node in an XML String

XML to be operated on:

   1: <States>

   2:   <State name="Wisconsin">

   3:     <Regions>

   4:       <Region name="Milwaukee">

   5:         <Area name="Mukwanago"/>

   6:         <Area name="Germantown"/>

   7:       </Region>

   8:       <Region name="Fox Valley">

   9:         <Area name="Oshkosh" />

  10:         <Area name="Appleton" />

  11:       </Region>    

  12:     </Regions>

  13:   </State>

  14: </States>

C# code:

   1: // using System.Xml.Linq

   2:  

   3: // make sure the xml file exists

   4: XElement states  = XElement.Load("testXML.xml");

   5:  

   6: // Using LINQ

   7: XElement foundNode;

   8: var query = from XElement r in states.Descendants("Region")

   9:                    where r.Attribute("name").Value == "Milwaukee"

  10:                    select r;

  11: foundNode = query.FirstOrDefault();

  12:  

  13: // Using Lambda expressions

  14: foundNode = states.Descendants("Region").

  15:      Where(r => r.Attribute("name").Value ==

  16:                          "Milwaukee").FirstOrDefault();