pierusch
pierusch

Reputation: 137

storing a database connection string into my.settings in a .net DLL data layer

Hy everybody I've always used this approach while implementing data layer DLL for web application:

1) put datasets into the dll project 2) use a helper class with a method like this:

public shared sub settCnnStr(strconnhere as string)
My.Settings.Item("connectionString") = strconnhere
end sub

3) when I need to use the dll I call a method like this (usually into global.asax):

xxxxxxx.helper.setCnnStr("yyyyyyyyyyyyyy")

This method always worked for me nicely but I'd like to know if this method could have serious drawback or if there's a better solution.

thanks

Pierluigi

Upvotes: 2

Views: 2380

Answers (1)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59963

I don't see any serious drawbacks with this approach.

However slightly better solution could be to specify the connection string in the configuration file and read it directly from your Class Library project through the My.Settings object.

How to read connection string settings with My.Setting

1) Add a connection string setting in the Visual Studio Settings Designer in your Class Library project. This will add it to the App.config configuration file for the project and generate a custom My.Settings class.

2) Since Class Library projects cannot read from App.config files, you'll have to move the generated connection string setting to your Web.config file:

<configuration>
    <connectionStrings>
        <add name="ClassLibrary.My.MySettings.MyConnString"
             connectionString="SomeConnString" />
    </connectionStrings>
</configuration>

3) This allows you to easily retrieve the connection string from your Class Library project this way:

Dim connString = My.Settings.MyConnString

Related resources:

Upvotes: 1

Related Questions