【WPF學習筆記】之如何點登錄按鈕時判斷用戶名密碼進行登錄:動畫系列之(二)


......

承接動畫系列之(一)的代碼:

再添加登錄按鈕代碼進行登錄,驗證用戶名和密碼在數據庫是否正確。

直接上代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using User.sqlHelper;
using System.Data;

namespace User
{
    /// <summary>
    /// uc_login.xaml 的交互邏輯
    /// </summary>
    public partial class uc_login : UserControl
    {
        public uc_login()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //獲取下拉框用戶名字
            User_test _u = new User_test();
            DataSet _ds = _u.GetList();
            if(_ds != null)
            {
                DataTable _dt = _ds.Tables[0];
                for (int i = 0; i < _dt.Rows.Count; i++)
                {
                    string UserName = _dt.Rows[i]["UserName"].ToString().Trim();
                    ComboBoxItem cbitem = new ComboBoxItem();
                    cb_uploader.Items.Add(cbitem);
                    cbitem.Content = UserName;
                }
            }
        }

代碼在這
private static string loginname = ""; //登錄按鈕 private void btn_login_Click(object sender, RoutedEventArgs e) { //判斷下拉框不為空 if (this.cb_uploader.Text != "") { //判斷密碼不為空 if (this.tb_password.Text != "") { //從下拉框獲取名字 string s1 = cb_uploader.Items[cb_uploader.SelectedIndex].ToString(); loginname = s1.Split(':')[1].Trim(); //從密碼框獲取密碼 string password = tb_password.Text.Trim(); //獲取數據庫的名字和密碼 User_test _u = new User_test(); DataSet _ds = _u.GetList(); if(_ds != null) { DataTable _dt = _ds.Tables[0]; for (int i = 0; i < _dt.Rows.Count; i++) { //數據庫取出name和psw string UserName = _dt.Rows[i]["UserName"].ToString().Trim(); string UserPassword = _dt.Rows[i]["UserPassword"].ToString().Trim(); //先判斷名字是否正確 if (loginname.Equals(UserName)) { //再判斷密碼是否正確 if (password.Equals(UserPassword)) { this.Visibility = Visibility.Collapsed;
                     c_login.IsHitTestVisible = false;
                     myHelper.mySendMessageToMainWindow(loginname);
                     return;
                                }
                                else
                                {
                                    MessageBox.Show("密碼不正確!");
                                    tb_password.Text = "";
                                }
                            }
                        }
                    }

                }
                //密碼為空
                else
                {
                    MessageBox.Show("請輸入密碼!");
                    return;
                }
            }
            //下拉框為空
            else
            {
                MessageBox.Show("請輸入用戶名!");
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM