• XML数据的读取—数据库配置文件


    数据库配置文件(config.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="ConnectionString" value="server=192.168.100.116;database=BDCY;uid=sa;pwd=123456;" />
        <add key="ConnString" value="server=(local);database=gxjd;uid=sa;pwd=" />
        <add key="BakData" value="W1" />
        <add key="dogServer" value="192.168.100.116">
        </add>
        <add key="dogport" value="9898">
        </add>
      </appSettings>
    </configuration>
    View Code

    引用命名空间(using System.Xml;)

    读取XML中的数据库连接字符串

       /// <summary>
            /// 读取XML中的数据数据库连接字符串
            /// </summary>
            /// <param name="value"></param>
            /// <param name="key"></param>
            /// <returns></returns>
            private string ConfigGetValues(string key)
            {
                string strRes = string.Empty;
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load("Config.xml");
                XmlNode xNode;
                XmlElement xElement;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElement = (XmlElement)xNode.SelectSingleNode("add[@key='"+key+"']");
                if (xElement != null)
                {
                    strRes = xElement.GetAttribute("value");
                }
                return strRes;
            }
    View Code

    写入XML中的数据库连接字符串

      /// <summary>
            /// 写入XML中的数据
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            private void ConfigSetValues(string key,string value)
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load("Config.xml");
                XmlNode xNode;
                XmlElement xElement;
                XmlElement xElement1;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElement = (XmlElement)xNode.SelectSingleNode("//add[@key='"+key+"']");
                if (xElement != null)
                {
                    xElement.SetAttribute("value", value); //"value"固定的AppStrings结点属性
                }
                else
                {
                    xElement1 = xDoc.CreateElement("add");
                    xElement1.SetAttribute("key", key);      //"key"固定的AppStrings结点属性
                    xElement1.SetAttribute("value", value);  //"value"固定的AppStrings结点属性
                    xNode.AppendChild(xElement1);
                }
                xDoc.Save("Config.xml");
            }
    View Code

    完整代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace Config
    {
        public partial class XMLCheck : Form
        {
            public XMLCheck()
            {
                InitializeComponent();
            }
    
            //读取数据
            private void btnRead_Click(object sender, EventArgs e)
            {
                GetConfigSub("ConnectionString");
            }
    
            //写入数据
            private void btnWrite_Click(object sender, EventArgs e)
            {
                SetConfigSub();
                MessageBox.Show("修改成功!");
            }
    
            /// <summary>
            /// 读取XML中的数据数据库连接字符串
            /// </summary>
            /// <param name="value"></param>
            /// <param name="key"></param>
            /// <returns></returns>
            private string ConfigGetValues(string key)
            {
                string strRes = string.Empty;
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load("Config.xml");
                XmlNode xNode;
                XmlElement xElement;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElement = (XmlElement)xNode.SelectSingleNode("add[@key='"+key+"']");
                if (xElement != null)
                {
                    strRes = xElement.GetAttribute("value");
                }
                return strRes;
            }
    
            /// <summary>
            /// 获取数据库连接的子信息
            /// </summary>
            private void GetConfigSub(string key)
            {
                string strRes = ConfigGetValues(key);
                string[] arr = strRes.Split(';');
                txbServerName.Text =arr[0].Substring(arr[0].IndexOf('=')+1);
                txbDBName.Text = arr[1].Substring(arr[1].IndexOf('=') + 1);
                txbAccountName.Text = arr[2].Substring(arr[2].IndexOf('=') + 1);
                txbDBPwd.Text = arr[3].Substring(arr[3].IndexOf('=') + 1);
                txbDogAddress.Text = ConfigGetValues("dogServer");
            }
    
            /// <summary>
            /// 写入XML中的数据
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            private void ConfigSetValues(string key,string value)
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load("Config.xml");
                XmlNode xNode;
                XmlElement xElement;
                XmlElement xElement1;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElement = (XmlElement)xNode.SelectSingleNode("//add[@key='"+key+"']");
                if (xElement != null)
                {
                    xElement.SetAttribute("value", value); //"value"固定的AppStrings结点属性
                }
                else
                {
                    xElement1 = xDoc.CreateElement("add");
                    xElement1.SetAttribute("key", key);      //"key"固定的AppStrings结点属性
                    xElement1.SetAttribute("value", value);  //"value"固定的AppStrings结点属性
                    xNode.AppendChild(xElement1);
                }
                xDoc.Save("Config.xml");
            }
    
            /// <summary>
            /// 设置数据库连接字符串
            /// </summary>
            private void SetConfigSub()
            {
                string serverName = "server=" + txbServerName.Text + ";";
                string dbName = "database=" + txbDBName.Text + ";";
                string dbAccount = "uid=" + txbAccountName.Text + ";";
                string dbPwd = "pwd=" + txbDBPwd.Text + ";";
                string dogServer = txbDogAddress.Text;
                string appSetting = serverName + dbName + dbAccount + dbPwd;
                ConfigSetValues("ConnectionString", appSetting);
                ConfigSetValues("dogServer", dogServer);
            }
        }
    }
    View Code

    效果图如下:

  • 相关阅读:
    高仿IOS下拉刷新的粘虫效果
    CSDN无耻,亿赛通无耻
    2014年10月Android面试总结
    HttpClient和HttpURLConnection的使用和区别(下)
    HttpClient和HttpURLConnection的使用和区别(上)
    Gson简单使用
    Android PowerImageView实现,可以播放动画的强大ImageView
    Android访问网络,使用HttpURLConnection还是HttpClient?
    自定义android ProgressDialog
    NPOI导Excel样式设置
  • 原文地址:https://www.cnblogs.com/zxd543/p/3566866.html
Copyright © 2020-2023  润新知