简体   繁体   中英

using connectionstring from web.config instead of app.config

I have a class library project in which I've added a dataset which talks with sql server. The output of this project is to be consumed from a web application project . So I intend to put my connection string in the web application project.

Did a couple of things. In order to make my adapter use a different connection string, I had come across this . But I finally found doing the following convenient:

Dim adapter as New MyReqeustTableAdapter()
adapter.Connection.ConnectionString = sMyConnectionString

Then I tried taking the connection string from my configuration (app.config) to kind of simulate . I added a section manually with key "myconstr". My ideas was to do something like:

sMyConnectionString = ConfigurationManager.ConnectionString("myconstr").ConnectionString

But my intellisense couldn't not detect ConfigurationManager. So had to add the appropriate reference to the project.

Next I added a connection string via the settings designer for the web application project. I gave the above key for referencing it. However the above statment seems to throw a null reference exception.

Suppose you have created a class library. In it you've defined a Settings property which goes like:

Properties.Settings.Default.ProjectName

Visual Studio might auto generate some configuration for you as follows:

(app.config) 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyDllProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <MyDllProject.Properties.Settings>
            <setting name="ProjectName" serializeAs="String">
                <value>MyDllproject</value>
            </setting>
        </MyDllProject.Properties.Settings>
    </applicationSettings>
</configuration>

Now say you add this assembly to an project. And you access its settings, you'd most probably get MyDllproject as it value. This is in spite of adding any configuration. Why? Because when the assembly was generated it was kind of written into it. And the code written is such that in the absence of a configuration override use what was defined in the app.config at the time of generation.

Now in your target project, you simply add the necessary sections in the configuration file in the following pattern

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>

        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <!-- start: copied from app.config of class library -->
            <section name="MyDllProject.Properties.Settings" 
                type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" 
                />
            <!-- end: copied from app.config of class library -->

            <!-- other sections may follow -->
        </sectionGroup>

    </configSections>

    <applicationSettings>
        <!-- remember to maintain the same order as how it was defined in the sectionGroup -->

        <!-- start: copied from app.config of class librarly -->
        <MyDllProject.Properties.Settings>
            <setting name="ProjectName" serializeAs="String">
                <value>ConsoleProjectB</value>
            </setting>
        </MyDllProject.Properties.Settings>
        <!-- end: copied from app.config of class library -->

        <!-- other configurations settings may follow -->
    </applicationSettings>
</configuration>

Thats it.

Here is a small project sample I've linked to for the same: http://sdrv.ms/16ksPef

您可以从类库项目中添加对System.Web引用并使用System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString)

A configuration file ( app.config ) which is defined in a class library project cannot be - automatically - consumed by the system. The only config file that can be consumed is the web.config file in web applications or *myexe.exe*.config in exe applications where exe file is *myexe.exe* .

So you seem to be trying to add app.config to the class library. That ain't gonna work.

It is possible to link a second config file to the web.config but that probbaly is no help to you.

Basically, what you need is a possibility to change a global parameter of your library from outside. Using app.config by itself does not give you that possibility straight away -- you have not exposed your library internal settings yet. With that in mind, one very straightforward way to achieve exposure is to create in your library an assigner for the app.config.

public static class Setter
{
    public static void Set(string name, string value)
    {
        Properties.Settings.Default[name] = value;
    }
}

Then, at web application start in global.asax, or elsewhere:

MyLibrary.Setter.Set("X", ConfigurationManager.ConnectionStrings["Y"].ConnectionString);

From your question I think you're building an n-layered application. I think you're better off the two options you mentioned.

You should just create a base class for your DAL (Data Access Layer) classes that will have a public property that contains that the connection string.

btw it will help you hide/secure your connection string instead of storing it in a file that could be easily read by anyone who have or gained access to your host

  public class DALBase { public static string connString { get { return "Data Source=localhost\\\\SqlExpress;Initial Catalog=theDb Integrated Security=True"; } } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM