如何更改应用程序配置文件中的连接字符串?

编程入门 行业动态 更新时间:2024-10-23 06:20:24
本文介绍了如何更改应用程序配置文件中的连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好 我有一个带有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[^]

更多推荐

如何更改应用程序配置文件中的连接字符串?

本文发布于:2023-11-15 23:53:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   配置文件   应用程序   如何更改

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!