【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("请输入用户名!");
            }
        }
    }
}

 

posted @ 2016-11-01 16:23  Owen_ET  阅读(2180)  评论(0编辑  收藏  举报