views:

402

answers:

3

how can I get reference to current SqlConnection or Sqlconnection in config?

I found http://svn.castleproject.org:8080/svn/castle/trunk/ActiveRecord/Castle.ActiveRecord.Tests/DifferentDatabaseScopeTestCase.cs

and code

 private string GetSqlConnection()
        {
            IConfigurationSource config = GetConfigSource();

            IConfiguration db2 = config.GetConfiguration(typeof(ActiveRecordBase));

            string conn = string.Empty;

            foreach (IConfiguration child in db2.Children)
            {
                if (child.Name == "connection.connection_string")
                {
                    conn = child.Value;
                }
            }

            return conn;
        }

But I cant understand where I can find "GetConfigSource" implementation? Is this standart Castle helper function or not?

I use these namespaces

using Castle.ActiveRecord;
using NHibernate.Criterion;
using NHibernate;
using Castle.Core.Configuration;
using Castle.ActiveRecord.Framework;
A: 

I had to doanload AR sources from svn to solve it.

It means

System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;

VoimiX
+1  A: 
var sfimpl = ActiveRecordMediator.GetSessionFactoryHolder()
                                 .GetSessionFactory(typeof(object));
IDbConnection conn = ((ISessionFactoryImplementor)sfimpl)
                        .ConnectionProvider.GetConnection();
Mauricio Scheffer
I had some problems with this method. But anyway, thank you
VoimiX
I think this is out of date. There is no *ConnectionProvider* in *ISessionFactory*.
oillio
@oillio: indeed. I just updated my answer, give it a try.
Mauricio Scheffer
A: 

The method I found to get the string was:

    public static string GetConnectionString() {
        using (var session = ActiveRecordMediator
                            .GetSessionFactoryHolder()
                            .GetSessionFactory(typeof(ActiveRecordBase))
                            .OpenSession())
        {
            return session.Connection.ConnectionString;
        }
    }

This may be inefficient to open a session to get the connection string. Is there another place to find it?

oillio