Question Sharing a Database Connection String between Classes in another Library

Joined
Nov 21, 2012
Messages
9
Programming Experience
3-5
I'm not sure if the title is very good, I'll explain.

Maybe I'm thinking about this too much, but lately I've been really trying to construct well designed classes and libraries.

Anyways, I'm trying to design a library which basically handles all of the communication to a particular database. So, for instance, as an example, I'm going to have an orders class, which will handle getting, updating information in an orders table, and an order_items class which will do the same. Now my question is, instead of passing a database connection string to the shared instance method of the class, or the class itself, is there a good way in which I can have this library (in a sense) instanced while my application runs so I could just make calls like Order.GetOrders() or OrderItems.GetItems() without passing a connection string everytime?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Absolutely. Connection strings should generally be stored in your app's config file. A library can get information from the current app's config file just the same way that the EXE itself can. Many libraries require an app that uses them to provide certain configuration data in the config file. You can do the same, e.g.
Dim connectionString = ConfigurationManager.ConnectionStrings.Item("MyLibraryConnectionString").ConnectionString
That code will work in any EXE or DLL as long as the current EXE has a connection string named "MyLibraryConnectionString" in its config file. As always, read the MSDN documentation for the ConnectionManager class to see what assembly and namespace it's in.
 
Joined
Nov 21, 2012
Messages
9
Programming Experience
3-5
Sorry, I'm not completely sure where what's supposed to be? So let's say I have my app, and my library. The app has the GUI, etc and the lib handles connections to the database. You're saying the connection string which I want to use with my lib should be stored in the app config file itself? And then how do I get the lib to use this automatically? Are you saying the lib should be calling to check the app for the connection string property? Sorry, that just doesn't make sense to me or maybe I'm not understanding something. But if you are saying that, then that means any application who used the dll would have to have this connection string as well, correct? Or the lib would be deemed useless? I'm kind of new to this whole, lib/app approach so this is all useful, thanks :)
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
Sorry, I'm not completely sure where what's supposed to be? So let's say I have my app, and my library. The app has the GUI, etc and the lib handles connections to the database. You're saying the connection string which I want to use with my lib should be stored in the app config file itself? And then how do I get the lib to use this automatically? Are you saying the lib should be calling to check the app for the connection string property? Sorry, that just doesn't make sense to me or maybe I'm not understanding something. But if you are saying that, then that means any application who used the dll would have to have this connection string as well, correct? Or the lib would be deemed useless? I'm kind of new to this whole, lib/app approach so this is all useful, thanks :)

That's exactly what I'm saying. As I said previously, there are many third-party libraries around that require any application that uses them to provide configuration data that way. If anyone but you might use the library then you need to document that requirement. Even if it's just you it's still not a bad idea. The code I posted earlier goes in the library wherever you need the connection string and, as long as the app using the library has that connection string in its config file, everything will just work as you'd expect.
 
Joined
Nov 21, 2012
Messages
9
Programming Experience
3-5
I didn't see a code snippet, or example at all, so I'm obviously confused. You seem rude, I'm not sure why you're being rude. Obviously if I'm still inquiring about the topic, I don't understand .. so? Sorry to bother you. Like, if you don't want to help more, don't.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
I didn't see a code snippet, or example at all, so I'm obviously confused. You seem rude, I'm not sure why you're being rude. Obviously if I'm still inquiring about the topic, I don't understand .. so? Sorry to bother you. Like, if you don't want to help more, don't.

What you think is obvious is not necessarily obvious. I answer a lot of questions on a lot of forums. If I didn't want to help I wouldn't be here and wouldn't have already given you the answer. I see a lot of people put in very little effort on their own behalf so when you asked a question that I had already answered it certainly appeared that you hadn't made the effort to read or understand post #2. If you didn't understand then you could have actually said that, e.g.
I read post #2 but I don't know how to use what you said. Can you provide some more details
That would have been obvious. Instead you just repeated a question that had already been answered and assumed that I would know what was in your head.

As someone who has been posting a lot on numerous forums for a number of years, I see a lot of people put insufficient effort into their questions. When that gets pointed out, some people stop and think about how they can improve their questions, thereby helping those who answer and therefore help themselves by increasing the chance that they'll get an answer and how quickly a relevant answer can be provided. Others want to feel hard done by and make the same mistakes again. Which you want to be is up to you.

If you still want an answer to your question then go back and read post #2 again. I gave you code to copy and paste and specifically stated that it would work in any EXE or DLL under the appropriate conditions. What exactly do you not understand? If you give me details then I can do the same for you.
 
Top Bottom