Passing connection string to DLL

J

Jeff

I have a main project which references several DLLs. Each DLL will
access the same database as the main project. I'm storing the
connection string within the main project's "web.config" file, so in
theory the DLLs do not have "visibility" to the connection string.

I considered creating a public sub within each DLL (class) which allows
the connection string to be set (but not read):

Public Class myClass
'DATA CONNECTION
Private mobjDataConnection As New SqlClient.SqlConnection

Public Sub SetConnectionString(ByVal strValue As String)
mobjDataConnection.ConnectionString = strValue
End Sub

...
...
End Class

This way, only the main project's Web.Config file needs to contain the
connection string. The main project will simply pass the string to the
"SetConnectionString" method of the class.

Is this secure? If not, any suggestions?
 
S

Steve C. Orr [MVP, MCSD]

Yes, this sounds like a good solution to me.
Another solution would be to have the middle tier (your business logic DLLs)
manage the connection string so the UI layer (your pages) wouldn't have any
visibility to it.
 
J

Jeff

"Another solution would be to have the middle tier (your business logic
DLLs)
manage the connection string so the UI layer (your pages) wouldn't have
any
visibility to it."

Thanks for the reply. Regarding your other recommended approach, how
would these DLLs have their own visibility to the main project's
"web.config" file? IOW, how would they know the connection string?
Would I have to give each project (DLL) its own web.config file?

I should clarify that each "business logic DLL" is being
developed/compiled within its own project which does *not* have a
"web.config" file.
 
S

Steve C. Orr [MVP, MCSD]

In this case the web.config might not be as easy, but any old XML file could
work just as well. You could make it as simple or complex as you need.
 
M

Mythran

Steve C. Orr said:
In this case the web.config might not be as easy, but any old XML file
could work just as well. You could make it as simple or complex as you
need.

You know, in your dll, you don't need to have SetConnectionString to get the
connection string that is stored in the <appSettings> section of the
web.config file?

All you need is to call
Configuration.ConfigurationSettings.AppSettings.GetConfig("ConnectionString")
to grab the key/value pair with the key ConnectionString. You can call this
from the DAL directly. This way, if you have a Windows Forms application
that needs to use the same BLL/DAL objects that the Web UI uses, it will
define the ConnectionString in it's app.config file and the DAL won't need
to be changed at all. Just calling the GetConfig will do the trick...

Not sure if you knew that or not :)

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top