1

如何在配置中获取对当前 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”实现?这个标准的城堡助手功能与否?

我使用这些命名空间

using Castle.ActiveRecord;
using NHibernate.Criterion;
using NHibernate;
using Castle.Core.Configuration;
using Castle.ActiveRecord.Framework;
4

4 回答 4

3
var sfimpl = ActiveRecordMediator.GetSessionFactoryHolder()
                                 .GetSessionFactory(typeof(object));
IDbConnection conn = ((ISessionFactoryImplementor)sfimpl)
                        .ConnectionProvider.GetConnection();
于 2009-10-01T14:53:32.397 回答
1

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

 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);
        }
    }
于 2010-12-02T04:17:49.040 回答
0

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

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

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

于 2010-04-29T00:39:48.837 回答
-1

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

它的意思是

System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
于 2009-10-01T14:50:59.407 回答