winform Config文件操作

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Reflection;

namespace ProcessErrorDataTools
{
public class ConfigManager
{
private static Configuration _configuration;
public static Configuration _Configuration
{
get
{
if (_configuration == null) _configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
return _configuration;
}
set { _configuration = value; }
}

public ConfigManager()
{

}

 

 

/// <summary>
/// 对[appSettings]节点依据Key值读取到Value值,返回字符串
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="key">要读取的Key值</param>
/// <returns>返回Value值的字符串</returns>
public static string ReadValueByKey(string key)
{
string value = string.Empty;
string filename = string.Empty;


//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);

XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点

得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");

if (element != null)
{
value = element.GetAttribute("value");
}

return value;
}

 

 

 

/// <summary>
/// 对[connectionStrings]节点依据name值读取到connectionString值,返回字符串
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="name">要读取的name值</param>
/// <returns>返回connectionString值的字符串</returns>
public static string ReadConnectionStringByName(string name)
{
string connectionString = string.Empty;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);
XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[appSettings]节点

得到[connectionString]节点中关于name的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");

if (element != null)
{
connectionString = element.GetAttribute("connectionString");
}

return connectionString;
}

 

 

 

/// <summary>
/// 更新或新增[appSettings]节点的子节点值,存在则更新子节点Value,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="key">子节点Key值</param>
/// <param name="value">子节点value值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool UpdateOrCreateAppSetting(string key, string value)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);

XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点

try
{
得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");

if (element != null)
{
//存在则更新子节点Value
element.SetAttribute("value", value);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("key", key);
subElement.SetAttribute("value", value);
node.AppendChild(subElement);
}

//保存至配置文件(方式一)
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}

isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}

return isSuccess;
}

 

 

 

/// <summary>
/// 更新或新增[connectionStrings]节点的子节点值,存在则更新子节点,不存在则新增子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="name">子节点name值</param>
/// <param name="connectionString">子节点connectionString值</param>
/// <param name="providerName">子节点providerName值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool UpdateOrCreateConnectionString(string name, string connectionString, string providerName)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);

XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[connectionStrings]节点

try
{
得到[connectionStrings]节点中关于Name的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");

if (element != null)
{
//存在则更新子节点
element.SetAttribute("connectionString", connectionString);
element.SetAttribute("providerName", providerName);
}
else
{
//不存在则新增子节点
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
subElement.SetAttribute("providerName", providerName);
node.AppendChild(subElement);
}

//保存至配置文件(方式二)
doc.Save(filename);

isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}

return isSuccess;
}

 

 

 

/// <summary>
/// 删除[appSettings]节点中包含Key值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="key">要删除的子节点Key值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteByKey(string key)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);

XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点

得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");

if (element != null)
{
//存在则删除子节点
element.ParentNode.RemoveChild(element);
}
else
{
//不存在
}

try
{
//保存至配置文件(方式一)
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}

isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}

return isSuccess;
}

 

 

 

/// <summary>
/// 删除[connectionStrings]节点中包含name值的子节点,返回成功与否布尔值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名称,枚举常量</param>
/// <param name="name">要删除的子节点name值</param>
/// <returns>返回成功与否布尔值</returns>
public static bool DeleteByName(string name)
{
bool isSuccess = false;
string filename = string.Empty;
//Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
XmlDocument doc = new XmlDocument();
filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + _Configuration.FilePath.Substring(_Configuration.FilePath.LastIndexOf("\\") + 1);
doc.Load(filename);

XmlNode node = doc.SelectSingleNode("//connectionStrings"); //得到[connectionStrings]节点

得到[connectionStrings]节点中关于Name的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");

if (element != null)
{
//存在则删除子节点
node.RemoveChild(element);
}
else
{
//不存在
}

try
{
//保存至配置文件(方式二)
doc.Save(filename);

isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}

return isSuccess;
}


}
}

转载于:https://www.cnblogs.com/hking911218/p/4460664.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 WinForms 应用程序中实现数据库连接,可以按照以下步骤进行操作: 1. 首先,确保你已经安装了适当的数据库驱动程序。根据你使用的数据库类型,下载并安装相应的驱动程序。例如,如果你使用的是 SQL Server 数据库,可以下载并安装 SQL Server 驱动程序。 2. 在项目中添加一个连接字符串来存储数据库连接的详细信息。在 WinForms 中,可以在 `app.config` 或 `web.config` 文件中定义连接字符串。打开你的项目文件,在 `<configuration>` 节点下添加一个 `<connectionStrings>` 节点,并在其中定义你的连接字符串。示例代码如下: ```xml <configuration> <connectionStrings> <add name="MyConnectionString" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> ``` 在上面的代码中,将 `Data Source` 替换为你的服务器名称,`Initial Catalog` 替换为你的数据库名称,`User ID` 和 `Password` 替换为你的数据库凭据。 3. 在你的 WinForms 窗体代码中,可以使用 `SqlConnection` 对象来建立与数据库的连接。首先,导入 `System.Data.SqlClient` 命名空间。然后,使用连接字符串名称从 `ConfigurationManager.ConnectionStrings` 集合中获取连接字符串。示例代码如下: ```csharp using System.Data.SqlClient; // ... string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { // 在这里执行与数据库相关的操作,如执行 SQL 查询、插入、更新等 } ``` 在上面的代码中,将 `"MyConnectionString"` 替换为你在 `app.config` 或 `web.config` 中定义的连接字符串的名称。 4. 通过使用 `SqlCommand` 和 `SqlDataReader` 等对象,可以执行 SQL 查询、插入、更新等操作。以下是一个示例代码,用于执行查询并读取结果: ```csharp string queryString = "SELECT * FROM TableName"; using (SqlCommand command = new SqlCommand(queryString, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // 在这里处理查询结果 } } } ``` 在上面的代码中,将 `"TableName"` 替换为你要查询的表名。 这样,你就可以在 WinForms 应用程序中成功实现数据库连接了。记得在使用完连接对象后,及时关闭连接以释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值