MQQ注册页面

来源:互联网 发布:js文本框内容改变事件 编辑:程序博客网 时间:2024/04/28 17:24

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyQQ
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
         //申请号码
        private void llblName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RegisterForm register = new RegisterForm();
            register.Show();
        }
        //退出
        private void TsimExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //方法
        private bool LoginValidate()
        {
            if (txtNum.Text.Trim() == "")
            {
                MessageBox.Show("请输入您的QQ号码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNum.Focus();          //定焦点
                return false;
            }
            else if (txtPwd.Text.Trim() == "")
            {
                MessageBox.Show("请输入您的QQ密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPwd.Focus();         //定焦点
                return false;
            }
            else
            {
                return true;
            }
        }

      
        public bool ValidateUser(string loginId, string loginPwd, ref string message)
        {
            int count = 0;            //返回结果
            //查询用的SQL语句
            string sql = string.Format("select count(*) from Users where Id='{0}' and LoginPwd='{1}'", loginId, loginPwd);
            try
            {
                //创建 Command 命名
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();          //打开连接
                count = (int)command.ExecuteScalar(); //执行查询语句

                //如果找到1个,验证通过,否则是非法用户
                if (count < 1)
                {
                    message = "用户名或密码不存在!";
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                message = "数据库出错!";
                Console.WriteLine(ex.Message); //出现异常,打印异常消息
                return false;

            }
            finally
            {
                DBHelper.connection.Close();   //关闭数据库连接
            }
        }
        //传头像方法
        private int Getfaceid()
        {
            int faceid = 0;
            string sql = string.Format("select faceid from users where id='{0}'", txtNum.Text);
            try
            {
                SqlCommand com = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader datareader = com.ExecuteReader();
                if (datareader.Read())
                {
                    faceid = Convert.ToInt32(datareader["faceid"]);
                }
                datareader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.connection.Close();
            }
            return faceid;
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            int faceid = Getfaceid();
            bool isValidUser = false;   //标识是否为合法用户
            string message = "";        //如果登录失败,显示的消息提示

            if (LoginValidate())
            {
                //调用用户验证方法
                isValidUser = ValidateUser(txtNum.Text, txtPwd.Text, ref message);
                if (isValidUser)
                {
                    //将输入的用户名保存到静态变量中
                    UserHeper.loginId = txtNum.Text;
                    //将输入的密码保存到静态变量中
                    UserHeper.loginPwd = txtPwd.Text;
                    UserHeper.faceid = faceid;
                    string sql = string.Format("select Id,NickName from Users where Id={0}", txtNum.Text);
                    //创建 Command 命名
                    SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                    DBHelper.connection.Open();          //打开连接
                    SqlDataReader dataReader = command.ExecuteReader();
                    string nickName = "";              
                    if (dataReader.Read())
                    {
                        nickName = dataReader["NickName"].ToString();                  
                    }
                    dataReader.Close();
                    //将读取到的登录用户的昵称保存到静态变量中
                    UserHeper.nickName = nickName;                 
                    DBHelper.connection.Close();
                     //调用主窗体
                    MainForm main = new MainForm();
                    main.Show();
                    this.Visible = false;           //隐藏当前窗体
                }
                else
                {
                    MessageBox.Show(message, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }
    
    
    }
}