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:
开发者_StackOverflow社区 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?
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()
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);
精彩评论