如何在C#中的app.config文件中直接更改或设置连接字符串 [英] How to change or set connection string directly in app.config file in C#

查看:118
本文介绍了如何在C#中的app.config文件中直接更改或设置连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将我的数据库文件放错到另一个文件夹,那么我想更改连接字符串路径。然后如何在运行时更改连接字符串。



我尝试过:



我必须在我的桌面应用程序中放置这种类型的功能。

If i misplace my database file to another folder then i want to change the connection string path. then how to change the connection string at run time.

What I have tried:

I have to put this type of functionality in my desktop application.

推荐答案

这比你要求的要多,但看看这里: 实例存储 - 在应用程序之间共享配置数据的简单方法 [ ^ ]

我有许多应用程序通过不同的数据库连接到同一个SQL实例,这段代码让我可以在一个地方更改SQL服务器连接字符串,它可以无缝地工作所有使用该代码的应用程序。
This is more than you are asking for, but have a look here: Instance Storage - A Simple Way to Share Configuration Data among Applications[^]
I have a number of apps which connect to the same SQL instance via different databases, and this code lets me change the SQL server connection string in one place and it seamlessly works for all the apps using the code.


您的app.config文件将在构建时重命名为 appname.exe.config ,您可以编辑此f ile使用文本编辑器或使用以下方法:

Your app.config file will be renamed to appname.exe.config when built, you can edit this file with a text editor or use methods like:
Properties.Settings.Default.FirstUserSetting = "abc";

更多信息:管理应用程序设置(.NET) [ ^ ]


这篇关于如何在C#中的app.config文件中直接更改或设置连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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