如何更改应用程序配置文件中的连接字符串? [英] how can i change connectionstring in application config file?

查看:125
本文介绍了如何更改应用程序配置文件中的连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个带有SQL EXPRESS数据库的应用程序。

我创建了一个连接到SQL SERVER的表单,有四个文本框。

textbox1 =服务器

textbox2 =数据库名称

textbox3 =用户

textbox4 =传递
我希望当我用文件填充文本框以更改app配置文件中的connectionstring中的数据时



任何人都可以为此提供示例



提前致谢



Hi guys

I have an application with SQL EXPRESS database.
I have created a form to connect to SQL SERVER with four textboxes.
textbox1 = server
textbox2 = database name
textbox3 = user
textbox4 = pass
I want when I fill the textboxes with data to change the data in connectionstring in app config file

can anyone give examples for this

Thanks in advance


推荐答案

使用[ApplicationNameSpace.Properties.Settings.Default.ConnectionStringName]访问app.config文件中的Connection字符串。



但我很担心,因为看起来你无法访问value属性来改变它,而只能读取它。



为什么不把你的connectionString保存到注册表?在那里你可以随时编辑/阅读
Use [ApplicationNameSpace.Properties.Settings.Default.ConnectionStringName] to access your Connection string in the app.config file.

But am worried, because it seems that there is no way you can access the value property in order to change it, instead you can only read the value.

Why don''t you instead save your connectionString to the registry? There you can edit/read it any time you want


using Microsoft.Win32;

//Writing to the registry

string ConnectionStringValues = TextBoxServer; + TextBoxUID;...; 

 RegistryKey regkey = Registry.CurrentUser.CreateSubKey(@"Software\YourSoftwareName\RegistryKey");
regkey.SetValue("ConnectionStringName", "ConnectionStringValues");

//Reading from the registry (Getting your connectionString)
RegistryKey regkey = Registry.CurrentUser.CreateSubKey(@"Software\YourSoftwareName\RegistryKey");
regkey.GetValue("ConnectionStringName").ToString();





/ *

别忘了先在注册表中创建注册表项

打开运行 - >输入命令regedit,

展开HKEY_CURRENT_USER,

找到并右键单击Software文件夹,



新 - >键(将其命名为ConnectionStringName或您喜欢的任何其他名称),

右键单击您创建的文件夹并选择:新建 - > gt;字符串值

您可以保留值字段为空白或使用当前连接字符串初始化



还要记住在构建安装程序时包含这些注册表



问候

* /



/*
Don''t forget to create the registry key first in the registry
Open Run -> Type in the command regedit,
Expand HKEY_CURRENT_USER,
Locate and RightClick on Software folder,

New -> Key (Name it ConnectionStringName or any other name you prefer),
Right Click on the folder you created and choose: New ->String Value
You can leave the value field blank or initialize it with your current connection string

Also remember to include these registries when building your installer

Regards
*/


请阅读:连接字符串和配置文件 [ ^ ]

LINQ to XML:在app.config中更改connectionString [ ^ ]



CP网站上的相关文章:

将连接字符串保存到app.config [ ^ ]

如何使用编码在App.config中添加/更改连接字符串 [ ^ ]
Please, read this: Connection Strings and Configuration Files[^]
LINQ to XML : Changing connectionString in app.config[^]

Related articles on CP site:
Saving Connection Strings to app.config[^]
How to Add/Change Connection String in App.config with Coding[^]


这篇关于如何更改应用程序配置文件中的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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