简体   繁体   English

VB .NET(.NET 2.0)类库配置

[英]VB .NET (.NET 2.0) Class Library configuration

I have created a class library in VB .NET. 我已经在VB .NET中创建了一个类库。 Some code in the library connects to the database. 库中的某些代码连接到数据库。 I want to create a config file that would hold the connection string. 我想创建一个包含连接字符串的配置文件。

I have created a "Settings.settings" file and stored the connection string in there. 我创建了一个“ Settings.settings”文件,并将连接字符串存储在那里。

When a class library having a settings file is built, it generates a ".dll.config" file which has the key value pairs defined in the settings file. 构建具有设置文件的类库时,它将生成一个“ .dll.config”文件,该文件具有在设置文件中定义的键值对。

Problem with this is when i change the connection string in the ".dll.config" file, the library does not references these changes. 问题是当我更改“ .dll.config”文件中的连接字符串时,库未引用这些更改。 I would still need to recompile the class library which would then overwrite my changes in the .dll.config file. 我仍然需要重新编译类库,然后该类库将覆盖.dll.config文件中的更改。

I need to be able to change the connection strings on the fly without having to recompile the library. 我需要能够在不重新编译库的情况下即时更改连接字符串。

Is there a mechanism in VB.NET class library (.NET 2.0) that would let me do this? VB.NET类库(.NET 2.0)中是否有一种机制可以让我做到这一点?

Passing the connection string to the class library from the web page that uses its method is not a option. 将连接字符串从使用其方法的网页传递到类库不是一种选择。

I have listed a sample below, this is how i would access the string. 我在下面列出了一个示例,这就是我访问字符串的方式。

Public Function getsettings(ByVal Setting As String) As String
        If Setting = "Demo" Then
            Return My.Settings.Demo
        Else
            Return My.Settings.Live
        End If
    End Function

If you have an application which uses your library called MyApp, then the connection string defined in MyApp.exe.config will be available to your library. 如果您有一个使用您的库的应用程序MyApp,则MyApp.exe.config中定义的连接字符串将对您的库可用。 Generally speaking the client program should set the configuration environment, not the library. 一般来说,客户端程序应设置配置环境,而不是库。

If GetApplicationSetting("connectionString") Is Nothing Then
    Throw New Exception("Could not retrieve connection string from .config file")
Else
    Return ConfigurationManager.AppSettings.Item("connectionString")
End If

Make sure you have the System.Configuration framework loaded to access the ConfigurationManager. 确保已加载System.Configuration框架以访问ConfigurationManager。

EDIT 1: If you are using it in a web-application, then set the connection string in web.config. 编辑1:如果您在Web应用程序中使用它,然后在web.config中设置连接字符串。

EDIT 2: If you set the connection string in the ConnectionStrings section of the .exe.config or web.config you can access it using : 编辑2:如果您在.exe.config或web.config的ConnectionStrings部分中设置了连接字符串,则可以使用以下命令进行访问:

ConfigurationManager.ConnectionStrings("MyConnectionString")

Config files are specific to the application. 配置文件特定于应用程序。 So if your DLL is used by an application, the app.config or web.config needs to have the entries you are trying to use in the DLL config. 因此,如果您的DLL被应用程序使用,则app.config或web.config需要具有您要在DLL配置中尝试使用的条目。

Unfortunately the "Not an option" is probably the correct option. 不幸的是,“非选项”可能是正确的选项。

We have multiple libraries that have the same requirement. 我们有多个具有相同要求的库。 We set it up so that our class library directly retrieves the connection string from the web.config file of the application that is using it. 我们对其进行设置,以便我们的类库直接从使用它的应用程序的web.config文件中检索连接字符串。 When you say: 当你说:

Passing the connection string to the class library from the web page that uses its method is not a option. 将连接字符串从使用其方法的网页传递到类库不是一种选择。

In theory, the web page is not passing the con str as a parameter, but the class library is just directly taking it from the web.config file. 从理论上讲,网页不是将con作为参数传递,而是类库只是直接从web.config文件获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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