C#_WindowsForms账户,密码连接数据库的登录界面

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


namespace WindowsFormsApplication1
{
    public partial class LoginForm2 : Form
    {
        public LoginForm2()
        {
            InitializeComponent();
        }


        private void Cancelbtn_Click(object sender, EventArgs e)
        {
            //调用窗口的 Close 方法,关闭窗口
            this.Close();
        }


        private void Acceptbtn_Click(object sender, EventArgs e)
        {
                if (this.Login())
                {
                    //定义数据库连接对象,初始值为null
                    SqlConnection aconneticon = null;
                    //定义数据库原文本命令对象,初始值为null
                    SqlCommand bcommand = null;


                    try
                    {
                        //定义连接字符串
                        string connectionString = "Server=(local);Database=MySchool;UID=sa;Pwd=123456";
                        //定义SQL语句
                        string sql = string.Format("SELECT COUNT(*) FROM Student WHERE LoginId = '{0}' AND LoginPwd = '{1}'", this.Accounttxt.Text, this.Passwordtxt.Text);


                        //创建连接对象,用于连接数据库
                        aconneticon = new SqlConnection(connectionString);
                        //创建连接对象,用于执行SQL语句
                        bcommand = new SqlCommand(sql,aconneticon);


                        //打开数据库连接
                        aconneticon.Open();


                        // 执行 SQL 语句,并返回结果集第一行第一列的值(1)。由于返回值是 object 类型,所以通过 Convert.ToInt32 进行转换。
                        int count = Convert.ToInt32(bcommand.ExecuteScalar());


                        if (count == 1)
                        {
                            //如果账户密码正确,则COUNT(*)查询结果为1,此时为登录成功。
                            MessageBox.Show("登录成功。");
                        }
                        else
                        {
                            //如果用户密码不正确,则COUNT(*)查询结果为0,此时为登录失败。
                            MessageBox.Show("账户或密码错误,请重新输入。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //全选密码输入框的文本
                            this.Passwordtxt.SelectAll();
                            //将焦掉转移到密码输入框
                            this.Passwordtxt.Focus();
                        }
                    }


                    catch(Exception ex)
                    {
                        //如果发生错误,则提示错误信息。
                        MessageBox.Show(ex.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                    finally
                    {
                        //释放SqlCommand命令对象。释放前先判断 bcommand 不为 null,这样代码更加充实
                        if (bcommand != null)
                        {
                            bcommand.Dispose();
                        }


                        //关闭SqlConneticon 连接对象。释放前先判断 aconneticon 不为 null。
                        if (aconneticon != null) 
                        {
                            aconneticon.Close();
                        }


                    }
                }
            }


        bool Login()
        {
            if (this.Accounttxt.TextLength == 0) 
            {
                MessageBox.Show("请输入账户。",  //对话框文本 
                    this.Text,                   //对话框标题(此处取窗口标题,作为对话框标题)
                    MessageBoxButtons.OK,        //对话框按钮(“确定”按钮)
                    MessageBoxIcon.Information); //对话框图标(“i”图标)


                //将焦点转移到账户输入框,方便用户输入账户。
                this.Accounttxt.Focus();


                //返回false,表示验证不通过
                return false;
            }
            else if (this.Passwordtxt.TextLength == 0)
            {
                MessageBox.Show("请输入密码。",  //对话框文本 
                    this.Text,                   //对话框标题(此处取窗口标题,作为对话框标题)
                    MessageBoxButtons.OK,        //对话框按钮(“确定”按钮)
                    MessageBoxIcon.Information); //对话框图标(“i”图标)


                //将焦点转移到密码输入框,方便用户输入密码。
                this.Passwordtxt.Focus();


                return false;
            }
            else
            {
                return true;
            }
        }


    }
}

你可能感兴趣的:(C#_WindowsForms)