-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Contrib: ConnectionStringsConnectionProvider for .NET 2.0
PostPosted: Thu Oct 27, 2005 4:55 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
A simple little class I whipped up. It's just a ConnectionProvider that uses the NHibernate hibernate.connection.connection_string setting to look up a corresponding connection string in the web.config or app.config.

Feel free to change the namespace, of course :)

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Connection;
using System.Data.OracleClient;
using System.Configuration;
using System.Web.Configuration;
using System.Data;
using NHibernate.Util;
using NHibernate;

namespace Stanford.Slac.NHibernateUtil.Connection
{
   /// <summary>
   /// Provides connections to NHibernate from connection strings stored
   /// in the standard ASP.NET 2.0 web.config "connectionStrings" section.
   /// </summary>
   public class ConnectionStringsSectionConnectionProvider : ConnectionProvider
   {
      /// <summary>
      /// Gets an IDbConnection based on the connection name set in
      /// the hibernate.connection.connection_string setting of your nHibernate config.
      /// </summary>
      /// <returns>An IDbConnection of the type defined by the NHibernate dialect.</returns>
      /// <remarks>
      /// NOTE: The type of the IDbConnection is determined by the NHibernate
      /// hibernate.dialect setting, *not* the "providerName" attribute on the "add" element
      /// in the "connectionStrings" section of the config.
      /// </remarks>
      /// <seealso cref="System.Configuration.ConnectionStringSettings">System.Configuration.ConnectionStringSettings</seealso>
      public override System.Data.IDbConnection GetConnection()
      {
         ConnectionStringSettings settings = null;
         if (null != System.Web.HttpContext.Current)
         {   
            settings = WebConfigurationManager.ConnectionStrings[this.ConnectionString];
         }
         if (null == settings)
         {
            settings = ConfigurationManager.ConnectionStrings[this.ConnectionString];
         }
         if (null == settings || String.IsNullOrEmpty(settings.ConnectionString))
         {
            throw new HibernateException("Could not configure NHibernate.",
               new ArgumentException("hibernate.connection.connection_string",
                  "'" + this.ConnectionString + "' did not correspond to any defined connections in " +
                  "the connectionStrings section of the config file."));
         }

         IDbConnection conn = this.Driver.CreateConnection();
         conn.ConnectionString = settings.ConnectionString;
         conn.Open();
         return conn;
      }
   }
}


Top
 Profile  
 
 Post subject: How to use?
PostPosted: Fri Dec 16, 2005 1:43 am 
Newbie

Joined: Sat Oct 15, 2005 3:30 am
Posts: 17
Hi,

I'm having a little trouble using this...

I have:
1. Added your class
2. adjusted the <add key="hibernate.connection.provider" value="" to point to this new class
3. removed the <add key="hibernate.connection.connection_string" value=".." entry from the nHibernate section
4. added a standard ASP.NET ConnectionStrings entry to config.

Is this what I should be doing?

I'm getting a "Could not find Connection String setting" error
If I add the connection_string key back into the nHibernate element it uses that connection string instead of from the asp.net style entry.

Thanks in advance,

Adam


Top
 Profile  
 
 Post subject: The answer
PostPosted: Fri Dec 16, 2005 1:47 am 
Newbie

Joined: Sat Oct 15, 2005 3:30 am
Posts: 17
Ahh me stoopid :)

You need to retain the nHibernate connection string setting, but for its value make it equal to the name of your asp.net connection string...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.