1

config で現在の 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」実装がどこにあるのかわかりませんか? これは標準の Castle ヘルパー機能ですか?

これらの名前空間を使用します

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 ソースを doanload する必要がありました。

その意味は

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