繁体   English   中英

如何在Castle ActiveRecord中获得对SqlConnection(或连接字符串)的引用?

[英]How to get reference to SqlConnection (or Connection string) in Castle ActiveRecord?

如何在配置中获取对当前SqlConnection或Sqlconnection的引用?

我发现http://svn.castleproject.org:8080/svn/castle/trunk/ActiveRecord/Castle.ActiveRecord.Tests/DifferentDatabaseScopeTestCase.cs

和代码

 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;
        }

但是我不明白在哪里可以找到“ GetConfigSource”实现? 这是Standart Castle助手功能吗?

我使用这些名称空间

using Castle.ActiveRecord;
using NHibernate.Criterion;
using NHibernate;
using Castle.Core.Configuration;
using Castle.ActiveRecord.Framework;
var sfimpl = ActiveRecordMediator.GetSessionFactoryHolder()
                                 .GetSessionFactory(typeof(object));
IDbConnection conn = ((ISessionFactoryImplementor)sfimpl)
                        .ConnectionProvider.GetConnection();

我在项目中像这样使用它:

 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);
        }
    }

我发现获取字符串的方法是:

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

这可能会导致打开会话以获取连接字符串的效率低下。 还有其他地方可以找到它吗?

我不得不从svn加载AR资源来解决它。

它的意思是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM