C# 如何在Castle ActiveRecord中获取对SqlConnection(或连接字符串)的引用?
如何在配置中获取对当前SqlConnection或SqlConnection的引用 我发现 和代码C# 如何在Castle ActiveRecord中获取对SqlConnection(或连接字符串)的引用?,c#,castle-activerecord,castle,C#,Castle Activerecord,Castle,如何在配置中获取对当前SqlConnection或SqlConnection的引用 我发现 和代码 private string GetSqlConnection() { IConfigurationSource config = GetConfigSource(); IConfiguration db2 = config.GetConfiguration(typeof(ActiveRecordBase));
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;
我必须从svn下载AR源才能解决它 这意味着
System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
我找到的获取字符串的方法是:
public static string GetConnectionString() {
using (var session = ActiveRecordMediator
.GetSessionFactoryHolder()
.GetSessionFactory(typeof(ActiveRecordBase))
.OpenSession())
{
return session.Connection.ConnectionString;
}
}
打开会话以获取连接字符串可能效率低下。还有别的地方可以找到它吗?我在项目中这样使用它:
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);
}
}
我认为这已经过时了。ISessionFactory中没有ConnectionProvider。这没有提供密码。@贾斯汀:问题不是关于任何密码。这是关于“如何获取对SqlConnection的引用”,我的帖子回答了这个问题。如果您想要更多/不同的内容,请创建一个新问题。这也不会给出密码。
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);
}
}