從無到有實現一個xml數據庫登錄驗証

來源:互聯網
上載者:User
        這兩天﹐對xml作為數據庫產生了興趣﹐找了一些資料﹐也搞出了一點眉目﹐在這裡記錄一下。算是對自己學習xml的一個小結吧。技朮內容不是很強﹐高手大俠們就不需看了。呵呵....
       不多說廢話﹐咱們程式員最注重的是實用性﹐以下就將本人自己產生xml數據庫﹐然後再登錄驗証的全過程共用出來。
       首先﹐請建立一個windows專案,然後從工具箱中拖兩個TextBox﹐ID分別為UserName 和UserPwd,然後再拖兩個Button出來﹐ID分別為btnOK和btnGen.Text屬性分別設為"驗証"和"建立"。
       然後在btnGen的click事件中加入如下代碼﹐產生一個xml檔案﹐作為數據庫﹕            XmlDocument xd = new XmlDocument();
            XmlNode xnDec = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            XmlElement xeRoot = xd.CreateElement("Users");
            xd.AppendChild(xnDec);
            xd.AppendChild(xeRoot);

            XmlElement xe1 = xd.CreateElement("Users");
            XmlElement xe1Name = xd.CreateElement("UserName");
            XmlElement xe1Pass = xd.CreateElement("UserPassword");
            xe1Name.InnerText = "Jack";
            xe1Pass.InnerText = "123";
            xeRoot.AppendChild(xe1);
            xe1.AppendChild(xe1Name);
            xe1.AppendChild(xe1Pass);

            XmlElement xe2 = xd.CreateElement("Users");
            XmlElement xe2Name = xd.CreateElement("UserName");
            XmlElement xe2Pass = xd.CreateElement("UserPassword");
            xe2Name.InnerText = "King";
            xe2Pass.InnerText = "123";
            xeRoot.AppendChild(xe2);
            xe2.AppendChild(xe2Name);
            xe2.AppendChild(xe2Pass);

            xd.Save(Application.StartupPath + "\\Users.xml");

       接著在btnOK的click事件中輸入如下代碼﹐作為驗証段﹐當然﹐我並沒有對xml檔案中的相關敏感資訊加密﹐畢竟只算是一個小的學習總結吧。            DataSet ds = new DataSet();
            ds.ReadXml(Application.StartupPath + "\\Users.xml");
            //DataView dv = new DataView();
            //dv = ds.Tables[0].DefaultView;
            //dv.Sort = "UserName";
            //dv.RowFilter = "UserName ='" + UserName.Text.Trim() + "'";
            DataTable dt = ds.Tables[0];
            DataRow[] dta = dt.Select("UserName='" + UserName.Text.Trim() + "'");

            //this.dataGridView1.DataSource = dv;
            if (dta != null && dta.Length > 0 )
            {
                DataRow dr = dta[0];
                string strPwd = (string)dr["UserPassword"];
                if (strPwd == this.UserPwd.Text.Trim())
                {
                    MessageBox.Show("OK");
                }
                else
                {
                    MessageBox.Show("No OK");
                }
            }
            else
            {
                MessageBox.Show("No this account");
            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.