Question

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;
Was it helpful?

Solution 4

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

It means

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

OTHER TIPS

var sfimpl = ActiveRecordMediator.GetSessionFactoryHolder()
                                 .GetSessionFactory(typeof(object));
IDbConnection conn = ((ISessionFactoryImplementor)sfimpl)
                        .ConnectionProvider.GetConnection();

I use it like this in my project:

 public static MySqlConnection GetConnection()
    {
        if (_session == null)
        {
            _session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(ActiveRecordBase));
        }
        var connection = (MySqlConnection)_session.Connection;
        if (connection.State == ConnectionState.Closed)
            connection.Open();
        //var connection = new MySqlConnection(Connstr);
        //connection.Open();
        return connection;
    }

    public static void CloseActiveIsession()
    {
        if (_session != null)
        {
            ActiveRecordMediator.GetSessionFactoryHolder().ReleaseSession(_session);
        }
    }

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top