更改连接字符串而不重新编译
我正在开发 WinForm 应用程序。我已将数据访问逻辑实现到“库项目”中,并将其设置为 WinForm 项目中的引用。我使用 LINQ to SQL 连接到我的项目数据库,将我使用的表映射到 dbml
文件中。现在我必须发布我的项目并更改连接字符串以指向生产数据库。
是否可以更改连接字符串而不重新编译项目? 它在调试时和维护时非常有用...
我尝试在 app.config
和 Settings
文件中更改它,但它看来还是指向开发DB。
我哪里做错了?
I'm working on a WinForm application. I've implemented data-access logic into a "library project" that I set as reference in my WinForm project. I'm using LINQ to SQL to connect to my project database, mapping the tables I use into a dbml
file. Now I have to publish my project and change the connection string to point to the production DB.
Is it possible to change the connection string without re-compile the project?
It'll be very useful at debug-time and for maintenance...
I've tried to change it in app.config
and also in the Settings
file, but it seems to still point to the development DB.
Where am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本文建议的解决方案非常好: http://goneale.com/2009/03/26/untie-linq-to-sql-connection-string-from-application-settings/
但我决定解决我的问题以不同的方式。
在不修改 dbml 文件中的任何内容的情况下,我在 DAO 类中添加了一个带有参数的构造函数:
然后而不是使用 DataClasses() 构造函数来实例化 LINQ-TO-SQL类,我用
DataClasses(_connString)
替换它。现在我可以在需要的地方使用数据访问库。连接字符串将在referencig应用程序的
app.config
中设置(或其他任何地方)。The solution suggested in this article is very good: http://goneale.com/2009/03/26/untie-linq-to-sql-connection-string-from-application-settings/
But I decided to solve my issue in a different way.
Without modifying anyting in the
dbml
file I added in my DAO class a constructor that takes a parameter:then instead of using
DataClasses()
constructor to instantiate the LINQ-TO-SQL class, I replaced it withDataClasses(_connString)
.Now I can use the data access library where I need. The connection string will be set in
app.config
of the referencig application (or anywhere else).