Raphael
Raphael

Reputation: 8192

Getting the connection string from nHibernate returns the string WITHOUT the password

I need to directly access the database on a nhibernate application. I am trying to get the connection string like this:

    using (SqlConnection conn = new SqlConnection(this.session.Connection.ConnectionString))

But this will return the connection string WITHOUT the password.

I'm I doing something wrong? Is there any other way to get the connection string?

Upvotes: 4

Views: 7118

Answers (2)

Arun Ayyappan
Arun Ayyappan

Reputation: 21

ISessionFactory SessionFactory = (NHibernate.Impl.SessionFactoryImpl)session.SessionFactory;//ISession session
        Type typeOfConnectionProvider = typeof(NHibernate.Connection.DriverConnectionProvider);
        PropertyInfo ConnectionStringPropertyInfo = typeOfConnectionProvider.GetProperty("ConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
        string connectionString = (string)ConnectionStringPropertyInfo.GetValue(((NHibernate.Connection.ConnectionProvider)((NHibernate.Impl.SessionFactoryImpl)SessionFactory).ConnectionProvider), null);

Upvotes: 2

JulianM
JulianM

Reputation: 1072

You can obtain a raw database connection (using the same connection details as NHibernate) from the NHibernate SessionFactory, e.g.:

IDbConnection connection = yourSessionFactory.ConnectionProvider.GetConnection()

Upvotes: 7

Related Questions