我如何...在应用程序配置文件中保存和编辑或更新和读取连接字符串 [英] How do I...save and edit or update and read connection string in the application configuration file

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

问题描述



请帮帮我





如何...保存,编辑或更新并在应用程序配置文件或app.config中读取连接字符串

解决方案

 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 

// StartPath表示app.config文件的路径通常出现在bin文件夹中
fileMap.ExeConfigFilename = StartPath + \\YourApp.exe.config;

//打开配置文件
配置config = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);

//用于从app.config文件中读取值
string KeyValue = config.AppSettings.Settings [KeyName]。Value;

//用于删除密钥
config.AppSettings.Settings.Remove(KeyName);

//添加密钥
config.AppSettings.Settings.Add(KeyName,Valuetopass);

//在Config文件中的任何更改之后需要保存如下
config.Save(ConfigurationSaveMode.Full);


< blockquote>要读取连接字符串,我有一个函数可以做到这一点。我传入连接的名称并返回连接字符串。



  public   string  GetConnectionStringsValue( string  key)
{
string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

string result = string .Empty;
if (!string.IsNullOrEmpty(key))
{
ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings [key];
if (connSettings!= null
{
result = connSettings.ConnectionString;
}
}

return 结果;
}


您好



可能以下链接对您有用。





在C#运行时配置App.Config文件中的连接字符串 [ ^ ]


hi
please help me


How do I...save and edit or update and read connection string in the "application configuration file" or app.config

解决方案

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

//StartPath means path of app.config file generally presents in bin folder
fileMap.ExeConfigFilename = StartPath + \\YourApp.exe.config;

// Open config file
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

//For Reading the values from app.config file
string KeyValue = config.AppSettings.Settings["KeyName"].Value;

//For removing the key
config.AppSettings.Settings.Remove("KeyName");

//For Adding the Key
config.AppSettings.Settings.Add("KeyName", "Valuetopass"); 

//After any changes in Config file need to save as follows
config.Save(ConfigurationSaveMode.Full);


To read a connection string I have a function that does that. I pass in the name of the connection and it returns the connection string.

public string GetConnectionStringsValue(string key)
        {
            string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            
            string result = string.Empty;
            if (!string.IsNullOrEmpty(key))
            {
                ConnectionStringSettings connSettings = ConfigurationManager.ConnectionStrings[key];
                if (connSettings != null)
                {
                    result = connSettings.ConnectionString;
                }
            }
            
            return result;
        }


Hello

May be the following link is useful to you.


Configuring a Connection String in the App.Config File During Runtime in C#[^]


这篇关于我如何...在应用程序配置文件中保存和编辑或更新和读取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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