更改可执行项目中包含的 DLL 中的连接字符串

发布于 2024-10-01 11:36:44 字数 960 浏览 2 评论 0原文

我有一个可执行的项目,并创建了一个 dll,其中包括项目(DLLforApp 和 App)中常用的数据集。

[DLLforApp]

  1. 与 App 不同的项目,但位于同一解决方案中。
  2. 这是一个类库项目,它创建一个 dll 文件,
  3. 该文件具有一个连接字符串,该连接字符串是 DLLforApp.Properties.Settings.Default.ConnectionString

[App]

  1. 同一解决方案中的另一个项目。
  2. 该项目包含一组数据集的引用..即 DLLforApp
  3. 该项目还有一个与 DLLforApp 相同的连接字符串。

现在,如果我编译此项目,将得到以下预期输出。

App.exe
DLLforApp.dll
App.exe.config 
...

我可以更改 App.exe.config 中 App.exe 的连接字符串,但如何更改 DLLforApp.dll 的连接字符串? 更改应用程序中的连接字符串不会影响 DLLforApp 的连接字符串..

所以,我现在要做的是..

在数据集设计器中选择 TableAdapter 部分并将属性 ConnectionModifier 从 Internal 更改为 Public。

在使用 DLLforApp.datasets 的表单中:

public Form1()

{

InitializeComponent();

customersTableAdapter1.Connection = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.MyConnection);

}

这将每次重置每个表适配器的连接。 这是唯一的方法吗?

谢谢,

I have a project that is executable and I created a dll, which includes commonly used datasets among projects (DLLforApp and App).

[DLLforApp]

  1. a separate project from App but in the same solution.
  2. this is a class library project which creates a dll file
  3. this has a connection string which is DLLforApp.Properties.Settings.Default.ConnectionString

[App]

  1. another project in the same solution.
  2. this project includes a reference for a set of datasets.. which is DLLforApp
  3. This project also has a connection string which is the same as DLLforApp.

Now, if I compile this project the following would be expected outputs..

App.exe
DLLforApp.dll
App.exe.config 
...

I can change the connection string for App.exe inside App.exe.config, but how do I change connection string for DLLforApp.dll ?
changing connection string in App does not affect the connection string for DLLforApp..

so, what I am doing now is..

In the DataSet designer select the TableAdapter section and change the property ConnectionModifier from Internal to Public.

In forms that use DLLforApp.datasets:

public Form1()

{

InitializeComponent();

customersTableAdapter1.Connection = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.MyConnection);

}

this will reset the connection for each tableadapter everytime..
is it the only way?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-10-08 11:36:44

在.net中,整个应用程序使用同一组配置条目。在您的 DLLforApp 项目中,您可以轻松地请求连接字符串,假设它将在某个地方定义,例如在 app.exe.config 中,或者如果您愿意,您可以创建一个 dllforapp.dll.config 文件,它将加载也来自该配置文件。

in .net, the entire application uses the same set of configuration entries. In your DLLforApp project, you can comfortably ask for a connection string, presuming it will be defined somewhere, like in the app.exe.config, or if you prefer, you can create a dllforapp.dll.config file, and it will load from that config file too.

菊凝晚露 2024-10-08 11:36:44

您需要为 dll 的配置添加一个部分,因此请将其添加到您的应用程序 .config 文件中。所以在 部分内... (主应用程序中会有一些)添加此内容:

<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <section name="MyNameSpace.MyDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>

然后,由于您已将组命名为“applicationSettings”,因此您需要在 < 下添加此内容;配置> ...

  <applicationSettings>
    <MyNameSpace.MyDll.Properties.Settings>
      <setting name="ConnectionString" serializeAs="String">
        <value>My connectionString</value>
      </setting>
    </MyNameSpace.MyDll.Properties.Settings>
  </applicationSettings>

这应该会给您一个良好的开端,我对需要为一个应用程序进行特殊配置的 DAL DLL 做了同样的事情 - 这是我从中复制的位置。

You need to add a section for the dll's config,so add this inside your applications .config file. So inside the section <configSections> ... </configSections> (There will be some there for you Main Application) add this:

<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <section name="MyNameSpace.MyDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>

and then since you've named the group 'applicationSettings' you'll need to add this under <configuration> ... </configuration>

  <applicationSettings>
    <MyNameSpace.MyDll.Properties.Settings>
      <setting name="ConnectionString" serializeAs="String">
        <value>My connectionString</value>
      </setting>
    </MyNameSpace.MyDll.Properties.Settings>
  </applicationSettings>

That should give you a head start, I've done the same thing for a DAL DLL that needed special config for one application - which is where I've copied this from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文