StrataFrame Forum

Sharing a database connection

http://forum.strataframe.net/Topic1480.aspx

By Andria Jensen - 6/9/2006

From what I can see, SF keeps a collection of Data Sources which are connections to the database that are available for use.  This is great for anything that has SF as its base in the application, which should be most things.  However, if there is some other third-party control that needs a database connection, or if I just wanted to directly access the open connection object in code how do I go about doing that?  Basically, I want to know how I can take an open db connection that SF has, and pass it to another object without it having to open up a new connection.
By Trent L. Taylor - 6/9/2006

Well, first let me explain that SF does not keep connections open for a longer period than required.  This produces system overhead and will slow down an application dramatically.  If you want to access the connection strings that are used by SF, you can reference the DataSources collection and create your own connection.  You can also create a DAL and have it open the connection for you, but this really is not recommended.

To manually create a connection you can do the following:

Dim loConnect As New System.Data.SqlClient.SqlConnection()

loConnect.ConnectionString = MicroFour.StrataFrame.Data.DataBasics.DataSources(0).ConnectionString
loConnect.Open()
loConnect.Close()

You really should allow the business object to do all of the work for you, even when using a 3rd party control.  Most any 3rd party control will have support for ADO.NET.  In this case, populate your BO and the use the CurrentDataTable, CurrentView, or CurrentRow property of the BO with the control.

If I have not answered your questions please let me know. Smile

By StrataFrame Team - 6/9/2006

There is also a method on the DbDataSourceItem object that creates new connections that can be used with the data source.

Dim loConnection As SqlConnection = DataLayer.DataSources("").CreateBlankDbConnection()

loConnection.Open()

'-- Do work

loConnection.Close()

You can also access a DbDataSourceItem from the DataSources collection by either key or by index.  Generally, you will want to use the key (string value) to retrieve the data source, as you never know what order the data sources are in.  If you only have one data source (generally you will), then you can access it fine by either DataSources("") or DataSources(0).

By Andria Jensen - 6/9/2006

Ok, I understand so far.  Consider that I have a parent MDI form, that basically just calls a bunch of other dll's like a lot of applications.  If I have a connection in the DataSources collection on my parent MDI, how do I pass it to the dll so that it knows about the databases available in that collection?  I know I can just pass a connection string and reset things, but I thought maybe there was an easier way?
By StrataFrame Team - 6/9/2006

If the other DLL is created by you, then simply add references to MicroFour StrataFrame Base and MicroFour StrataFrame Business assemblies.  Then the DLL will be able to see the DataLayer.DataSources collection (it's a shared (static) collection, like a global variable).
By Andria Jensen - 6/9/2006

Awesome, I didn't realize that but it makes total sense know that I know.  I guess I was just missing that part completely.  Thanks for the help!
By StrataFrame Team - 6/9/2006

No problem Smile  That's why we made the collection shared... so it would be visible from anywhere.