C#创建配置文件

时间:2010-12-29 12:05:59

标签: c#-4.0

此代码如何创建配置文件,以便我可以轻松更改连接字符串

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 MySql.Data.MySqlClient;
using System.Web;
using mshtml;


namespace tabcontrolweb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

         }


        private void Form1_Load(object sender, EventArgs e)
        {
            string MyConString = "SERVER=192.168.0.78;" +
                 "DATABASE=webboard;" +
                 "UID=aimja;" +
                 "PASSWORD=aimjawork;" +
                 "charset=utf8;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "SELECT  urlwebboard FROM `listweb` WHERE `urlwebboard` IS NOT NULL AND ( `webbordkind` = 'เว็บท้องถิ่น' ) and `nourl`= 'n' order by province, amphore limit 4 ";
            connection.Open();
            Reader = command.ExecuteReader();


            string[] urls = new string[4];
            string thisrow = "";
            string sumthisrow = "";

            while (Reader.Read())
            {
                thisrow = "";

                for (int i = 0; i < Reader.FieldCount; i++)
                {
                    thisrow += Reader.GetValue(i).ToString();


                    System.IO.File.AppendAllText(@"C:\file.txt", thisrow + " " + Environment.NewLine);
                    sumthisrow = Reader.GetValue(Reader.FieldCount - 1).ToString();

                }

                for (int m = 0; m < 4 ; m++)
                {
                    urls[m] = sumthisrow;
                    MessageBox.Show(urls[m]);          
                }

                webBrowser1.Navigate(new Uri(urls[0]));
                webBrowser1.Dock = DockStyle.Fill;
                webBrowser2.Navigate(new Uri(urls[1]));
                webBrowser2.Dock = DockStyle.Fill;
                webBrowser3.Navigate(new Uri(urls[2]));
                webBrowser3.Dock = DockStyle.Fill;
                webBrowser4.Navigate(new Uri(urls[3]));
                webBrowser4.Dock = DockStyle.Fill;


            }



            connection.Close();


        }



        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //if (webBrowser1.Document != null)
            //{
            //    IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
            //    if (document != null)
            //    {
            //        IHTMLSelectionObject currentSelection = document.selection;

            //        IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
            //        if (range != null)
            //        {
            //            const String search = "We";

            //            if (range.findText(search, search.Length, 2))
            //            {
            //                range.select();
            //            }
            //        }
            //    }
            //}
        }


    }
}

3 个答案:

答案 0 :(得分:3)

您可以创建如下所示的XML配置文件:

<db-config>
    <server>192.168.0.78</server>
    <database>webboard</database>
    <...>...</...>
</db-config>

然后,使用XMLTextReader来解析它。

以下是如何使用它来解析XML文件的基本示例:

using System;
using System.Xml;
namespace ReadXMLfromFile
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        static void Main(string[] args)
        {
            XmlTextReader reader = new XmlTextReader ("books.xml");
            while (reader.Read()) 
            {
                switch (reader.NodeType) 
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine (reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}

提示:使用ReadTofollowing()获取您的值。

完成XML DB Config Reader类之后,每次需要新连接时都会使用它,并且只需更改DB Config XML文件即可更改数据库连接配置。

编辑:这里有一篇关于Storing database connection settings in .NET的有趣文章。

答案 1 :(得分:2)

使用默认的
System.Configuration.ConfigurationManager

C#支持!

答案 2 :(得分:0)