-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Dynamics connection strings: how to programmatically specify
PostPosted: Mon Jun 26, 2006 6:00 pm 
Newbie

Joined: Mon Jun 26, 2006 5:13 pm
Posts: 1
I am trying to programmatically set the connection string, as I am in a multi-company, multi-database scenario. I found this article on doing so in Java, but I am not having any luck with NHibernate.

http://www.hibernate.org/386.html

Here is my App.config file:
<nhibernate>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.isolation" value="ReadCommitted" />
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
</nhibernate>

Here is my sample code:
// Specify connection string.
string dsn = "connection string goes here";

// Initialize NHibernate Session Factory.
Configuration cfg = new Configuration();
cfg.AddAssembly("AssemblyName");
cfg.SetProperty("hibernate.connection.connection_string", dsn);
ISessionFactory factory = cfg.BuildSessionFactory();

// Open a session.
ISession session = factory.OpenSession();

The last line throws this error: Could not find connection string setting.

If I do specify the key/value pair for the connection string in the App.config, I get this error: cannot open connection.

So my code is still trying to load the connection string from the App.config file, even though I have tried to override this value programmatically with the Configuration object.

I must be using the wrong objects, doing things in the wrong order, or I'm just plain way off track.

I have extracted the threading code and other infrastructure out of this example, and I don't believe that additional code is the problem.

Does anyone have any idea on how to approach this? Any help would be much appreciated.

Thanks,
Michael


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 1:15 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I just tried mine using Configuration.SetProperty() and it seems to work just fine (my code does close to exactly what you are doing). It sounds like there might be something wrong with the connection string itself since you said that it doesn't work if you specify the connection string in the config file either.

BTW, there's another slightly different syntax that allows you to specify NHibernate settings in app.config. It also lets you specify what assemblies you want classes loaded from. If you do a search for <configuration> in the following document, you'll see it.

http://www.hibernate.org/hib_docs/nhibe ... ml_single/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 1:33 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Interestingly, I just noticed that if I use the other method of specifying the properties, i.e. using a hibernate-configuration section instead of a nhibernate section, it doesn't appear to read my properties. It says that the dialect isn't set. I guess I'll just stick with what I've been doing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 8:28 am 
Newbie

Joined: Tue Jun 27, 2006 11:43 am
Posts: 1
this works for me

Code:
         cfg = new Configuration();
         cfg.Properties["hibernate.connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
         cfg.Properties["hibernate.dialect"] = "NHibernate.Dialect.MsSql2000Dialect";
         cfg.Properties["hibernate.connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
         cfg.Properties["hibernate.connection.connection_string"] = ConnectionString;
         //cfg.Properties["hibernate.connection.isolation"] = "ReadCommitted";

         try
         {
            cfg.AddAssembly( "Assembly" );
            sf = cfg.BuildSessionFactory();
         }
         catch (Exception e)
         {
//...
         }


with NO <nhibernate> section in App.Config


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 12:56 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I found out what my problem was. If you use a <hibernate-configuration> section, you need to call Configuration.Configure() for it to read the settings out of App.config. If you use an <nhibernate> section, you don't.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.