Home  >  Article  >  Backend Development  >  Implement an xml database login verification from scratch

Implement an xml database login verification from scratch

黄舟
黄舟Original
2017-02-27 16:40:071407browse

        這兩天﹐對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
"
);
            }

 以上就是从无到有实现一个xml数据库登录验证的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn