关键词:
【中文标题】在 C# 运行时读取、写入和更新 app.config 文件中的连接字符串【英文标题】:read,write and update connectionstring in app.config file at run time in C# 【发布时间】:2017-08-12 15:01:03 【问题描述】:我为连接设置创建了一个表单,用户可以在其中更新服务器名称、数据库以及用户 ID 和密码。我已将连接字符串存储在 app.config 文件中。
我的问题是,如何在运行时更新app.config文件中的连接字符串以及如何在第一时间通过表单上的文本框更改连接字符串的信息,文本框将显示信息第一次后的服务器名、id、密码
这里是我的 app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Data123.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="Data123Entities" connectionString="metadata=res://*/Data123DB.csdl|res://*/Data123DB.ssdl|res://*/Data123DB.msl;provider=System.Data.SqlClient;provider connection string="data source=nguyenduyhai;initial catalog=Data123;persist security info=True;user id=sa;password=1234567;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<!--<add name="Data123Entities" connectionString="metadata=res://*/Data123DB.csdl|res://*/Data123DB.ssdl|res://*/Data123DB.msl;provider=System.Data.SqlClient;provider connection string="data source=NGUYENDUYHAI\SQLEXPRESS;initial catalog=Data123;persist security info=True;user id=sa;password=1234567;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />-->
</connectionStrings>
</configuration>
这里是我正在尝试但不起作用的代码,请帮助我
void saveconect(string address,string id,string pass)
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["Data123Entities"].ConnectionString = "data source=" + address + ";initial catalog=" + "Data123" + ";user id=" + id + ";password=" + pass + "";
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
private void buttonUpdate_Click(object sender, EventArgs e)
saveconect(textboxServerAddress.Text, textBoxUserID.Text, textBoxPassword.Text);
【问题讨论】:
从配置中读取很容易,但使用 Configuration Manager 命名空间写入它们总是很麻烦。直接使用 XML 即可:***.com/a/37163251/495455 你能指导我详细一点吗,如果你能指导我,我很感激 请参考以下链接***.com/questions/502411/… 【参考方案1】:您是否尝试在发布文件夹中运行应用程序? 因为在 Debug 模式下不会改变。
【讨论】:
当然我在发布文件夹中运行它但仍然没有工作,你认为我的代码有错误吗?【参考方案2】:使用 System.Xml.Linq; 会是这样的:
var doc = XElement.Load(fileName);
var target = doc.Element("configuration").Elements("configurationStrings").Where(e => e.Element("name").Value == "Data123Entities").Single();
target.Element("connectionString").Value = "metadata=res://*/Data123DB.csdl|res://*/Data123DB.ssdl|res://*/Data123DB.msl;provider=System.Data.SqlClient;provider connection string="data source=" + dataSource + ";initial catalog=" + initCatalog + ";persist security info=True;user id=sa;password=1234567;MultipleActiveResultSets=True;App=EntityFramework""
doc.Save(fileName);
【讨论】:
我的文件名 = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 但我知道了,我该怎么办 System.NullReferenceException is unhandled Message: An unhandled exception of type 'System.NullReferenceException' occurred in ShinEtsu.exe Additional信息:对象引用未设置为对象的实例。 Object reference not set to an instance of an object. 是最流行的 .net 错误消息...对不起,您必须通过谷歌搜索解决它......它太容易了 - 单步执行代码,直到你得到异常并在它失败的那一行查看什么对象为空!!! 最后一个问题你给我这个 var target = doc.Element("configuration").Elements("configurationStrings").Where(e => e.Element("name").Value = = "Data123Entities").Single();但我在上面的 app.config 中没有看到任何“configurationStrings”部分是错误还是我遗漏了什么?使用 IPC C# 时如何有效地从管道流中读取
...子进程(进程B)。我使用匿名管道来写入有关在进程B上运行的方法的进度的信息。同时,我在进程A中有一个函数,它不断地从流中读取,以查看是否有来自管道的新更新。如果有,则更新流程 查看详情
在 C#、Java 和 PHP 中读取 Excel 文件和评估公式时,我都有哪些选择?
】在C#、Java和PHP中读取Excel文件和评估公式时,我都有哪些选择?【英文标题】:WhatoptionsdoIhaveforreadingExcelfilesandevaluatingformulasinC#,JavaandPHP?在C#、Java和PHP中读取Excel文件和评估公式时,我有哪些选择?【发布时间】:2009-01-1914:15:4... 查看详情
如何在 C# .NET 中保持 TCP 连接打开并执行多次写入/读取?
】如何在C#.NET中保持TCP连接打开并执行多次写入/读取?【英文标题】:HowtokeepaTCPconnectionopenandperformmultipleWrites/ReadsinC#.NET?【发布时间】:2021-12-1611:21:25【问题描述】:有多个帖子描述了保持TCP连接打开而不是在每次需要读取或... 查看详情
如何在 C# 和 ASP.NET MVC 中读取/写入 cookie
】如何在C#和ASP.NETMVC中读取/写入cookie【英文标题】:Howcanread/writecookieinC#&ASP.NETMVC【发布时间】:2019-01-0518:00:44【问题描述】:我的cookie有问题,这是我在课堂上的读写代码:publicstaticclasslanguagepublicstaticvoidset_default(stringname)Ht... 查看详情
读取错误 C#
...其写入文件,然后再从中读取。代码摘录如下。应用程序运行良好并保存文件。但是,当它尝试读取文件时,应用程序停止工作并且我收到错误“LiveRecognition 查看详情
如何在 C# 中调用多个控件
...计)。程序调用外部(第3方)DLL函数与硬件通信,并从运行在不同线程上的DLL回调更新 查看详情
写入/读取文本文件 (C#)
】写入/读取文本文件(C#)【英文标题】:Write/ReadTextFiles(C#)【发布时间】:2015-10-1621:08:40【问题描述】:我不是100%确定如何在标题中解释这一点或如何搜索它,如果之前已经发布过,非常抱歉。我正在尝试做的是读取和写入文件... 查看详情
c#数组快速读取和写入
摘要:C#数组快速读取和写入1BinaryFormatterbinaryFormatter=newBinaryFormatter();2FileStreamfileStream=newFileStream(@"E:context.txt",FileMode.Create);3binaryFormatter.Serialize(fileStream,a);4fileStream.Dispose() 查看详情
c#读取和写入xml文件
...平时开发的过程中,这块内容最主要的是要掌握XML内容的读取和写入操作。一.什么是XML?XML 指可扩展标记语言(EXtensible Markup Language)XML 是一种标记语言,很类似HTMLXML 的设计宗旨是传输数据,而非显示数据XML... 查看详情
在运行应用程序时将数据写入 plist 并将它们读取到 UITable
】在运行应用程序时将数据写入plist并将它们读取到UITable【英文标题】:WritedatatoplistwhilerunningappandreadthemtoUITable【发布时间】:2013-11-1020:49:25【问题描述】:我想将数据存储到UITable的plist中,但到目前为止,如果它们已经写在那... 查看详情
c#二进制文件的写入和读取
c#二进制文件的写入和读取,参考代码如下:using System; using System.IO; public class FileApp public static void Main() // 在当前目录创建一个文件myfile.txt,对该文件具有读写权限 FileStream fsMyfi... 查看详情
在 C# 中在运行时创建 pdf 文件 [关闭]
】在C#中在运行时创建pdf文件[关闭]【英文标题】:Creatingpdffilesatruntimeinc#[closed]【发布时间】:2010-10-0216:48:36【问题描述】:是否有附加/可以附加到.NET3.5的pdf库,它允许在运行时创建pdf文件,即打开一个新的pdf文件,逐行写入... 查看详情
在 C# 中在运行时创建 pdf 文件 [关闭]
】在C#中在运行时创建pdf文件[关闭]【英文标题】:Creatingpdffilesatruntimeinc#[closed]【发布时间】:2010-10-0216:48:36【问题描述】:是否有附加/可以附加到.NET3.5的pdf库,它允许在运行时创建pdf文件,即打开一个新的pdf文件,逐行写入... 查看详情
如何在 .Net 框架中长时间读取和写入 Azure 中的控制台?
...有一个.netframework4.5.1MVCweb应用程序。我们希望在应用程序运行时将信息记录到控制台。显然,在我的本地计算机上运行应用程序时,我可以看 查看详情
如何在不使用用户设置的情况下在运行时读取/写入 app.config 设置?
】如何在不使用用户设置的情况下在运行时读取/写入app.config设置?【英文标题】:HowcanIread/writeapp.configsettingsatruntimewithoutusingusersettings?【发布时间】:2011-04-0800:20:53【问题描述】:我正在寻找一种方法来存储可以在运行时使用A... 查看详情
c++和c#之间的共享内存同步
...从共享内存读取字符串并将其写入控制台的C#程序。程序运行良好,但目前我在定时循环中写入字符串,然后以相同的频率从另一个定时循环中读取它。我想做的是创建某种锁定。我想使用信号量/互斥体/任何东 查看详情
c# 访问 c++ dll 时尝试读取或写入受保护的内存错误
】c#访问c++dll时尝试读取或写入受保护的内存错误【英文标题】:Attemptedtoreadorwriteprotectedmemoryerrorwhilec++dllisbeingaccessedbyc#【发布时间】:2012-06-0417:56:43【问题描述】:下面是c++dll类classApublic:int__thiscallcheck(char*x,char*y,char*z);private:B*... 查看详情
c# - 在运行时更改 App.Config 后,实体框架 ConnectionString 不会更新
】c#-在运行时更改App.Config后,实体框架ConnectionString不会更新【英文标题】:c#-EntityFrameworkConnectionStringwon\'tupdateafterchangingApp.Configinruntime【发布时间】:2015-08-2813:58:36【问题描述】:我正在开发一个WPF应用程序,它依赖于实体框... 查看详情