连接字符串的存储和重用 [英] Connection Strings storage and reuse

查看:48
本文介绍了连接字符串的存储和重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在VS2012中有多个项目的解决方案.每个项目都需要访问相同的数据库.我有一个DAL,可以处理所有连接和数据检索.

我要做的是(1).以DAL在运行时和设计时可以访问它的方式存储连接字符串(我拥有的某些WPF表单具有绑定到ViewModels的绑定,该ViewModels在设计时从数据库中检索数据,ConfigurationManager.ConnectionStrings作为app.config返回null.位于另一个项目中)和(2).如果将来要将数据库移到另一台服务器,我想更新我的连接字符串而不必重建我的解决方案(配置文件或模块在运行时更改和保存连接字符串吗?)

我不关心加密连接字符串.

任何建议或示例,将不胜感激.

谢谢!

解决方案

您还可以将连接字符串存储在文本文件中,并在需要时可以通过编程方式读取文本文件来访问它.


app.config应该是每个应用程序.

您可以将连接字符串存储在单独的text/xml文件中.确保在设计时该文件可用.只要文件存在并进行适当的引用,绑定就可以工作.

要在运行时响应更改,您需要添加文件监视程序 [ ^ ]到配置文件.

这类似于log4net监视配置文件的方式. http://stackoverflow.com/a/372​​739/649524

Hi,

I have a solution with multiple projects in VS2012. Each project needs access to the same database. I have a DAL which handles all connections and retrieval of data.

What I do need to do is (1). store the connection string in such a way that the DAL can access it during runtime and design time (some WPF forms I have, has bindings to ViewModels which retrieve data from the database during design time and ConfigurationManager.ConnectionStrings returns null as the app.config is located in another project) and (2). If the database is moved to another server in the future, I would like to update my connection string without having to rebuild my solution (config file or module to change and save the connection string during runtime?)

I am not concerned about encrypting the connection string.

Any advice or examples would be appreciated.

Thanks!

解决方案

You can also store the connection string in a text file, and can access it when needed by reading the text file programatically


app.config is supposed to be per application.

You can store connection string in separate text/xml file. Make sure this file is available during design time. Bindings will work, as long as file exists, and referenced appropriately.

To respond to changes at run time, You need to add a file watcher[^] to the config file.

This is similar to how log4net watches config file.http://stackoverflow.com/a/372739/649524


这篇关于连接字符串的存储和重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆