更改可执行项目中包含的 DLL 中的连接字符串
我有一个可执行的项目,并创建了一个 dll,其中包括项目(DLLforApp 和 App)中常用的数据集。
[DLLforApp]
- 与 App 不同的项目,但位于同一解决方案中。
- 这是一个类库项目,它创建一个 dll 文件,
- 该文件具有一个连接字符串,该连接字符串是 DLLforApp.Properties.Settings.Default.ConnectionString
[App]
- 同一解决方案中的另一个项目。
- 该项目包含一组数据集的引用..即 DLLforApp
- 该项目还有一个与 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]
- a separate project from App but in the same solution.
- this is a class library project which creates a dll file
- this has a connection string which is DLLforApp.Properties.Settings.Default.ConnectionString
[App]
- another project in the same solution.
- this project includes a reference for a set of datasets.. which is DLLforApp
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在.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.
您需要为 dll 的配置添加一个部分,因此请将其添加到您的应用程序
.config
文件中。所以在部分内...
(主应用程序中会有一些)添加此内容:然后,由于您已将组命名为“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:and then since you've named the group 'applicationSettings' you'll need to add this under
<configuration> ... </configuration>
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.