windows窗体

来源:互联网 发布:邮箱绑域名有什么用 编辑:程序博客网 时间:2024/06/01 07:23
sing 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.Threading.Tasks;
using System.Windows.Forms;
namespace 第一章
{
    public partial class frmMain : Form
    {
        //连接字符串
        private const string sqlcon = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Pwd=.";
        //用于打开/关闭数据库连接
        SqlConnection con = new SqlConnection(sqlcon);
        //构造方法:用来做初始化工作
        public frmMain()
        {
            InitializeComponent();
        }
        //取消按钮的单击事件
        private void btnexit_Click(object sender, EventArgs e)
        {
            DialogResult result= MessageBox.Show("是否取消?","提示",MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                //关闭窗体
                this.Close();
            }
            else
            {
                //清空窗体中所有信息
                this.txtName.Clear();
                this.txtPwd.Clear();
                this.cborow.SelectedIndex = -1 ;
            }
           
          
        }
        //登录按钮的单击事件
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
               
                try
                {
                  
                    con.Open();
                    string sql = "select count(*) from Student where StudentNo='" + this.txtName.Text + "' and LoginPwd='" + this.txtPwd.Text + "'";
                    SqlCommand com = new SqlCommand(sql, con);
                    int count = (int)com.ExecuteScalar();
                    if (count > 0)
                    {
                        MessageBox.Show("登陆成功!", "提示");
                        //创建出FrmTest窗体
                        FrmTest test = new FrmTest();
                        //test代表的就是FrmTest窗体    .Test就是标题
                        test.txtshow.Text = "欢迎您:" + this.txtName.Text;
                        test.Show();
                      
                    }
                    else
                    {
                        MessageBox.Show("登陆失败!", "提示");
                        this.txtName.Clear();
                        this.txtPwd.Clear();
                        this.cborow.SelectedIndex = -1;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    con.Close();
                }
              
            }
        }
        //在点击登录按钮的时候验证文本框内容是否为空
        private bool CheckInput()
        {
            bool flag = true;
            //判断文本框是否输入值了?
            //判断txtName是否输入值了
            if (this.txtName.Text.Trim()== null||this.txtName.Text.Trim() =="")
            {
                //提示信息
                MessageBox.Show("请输入登录账号!","登录验证");
                flag = false;
            }else if (this.txtPwd.Text.Trim() == null || this.txtPwd.Text.Trim() == "")
            {
                //提示信息
                MessageBox.Show("请输入登录密码!", "登录验证");
                flag = false;
            }
            else if (this.cborow.Text.Trim() == null || this.cborow.Text.Trim() == "")
            {
                MessageBox.Show("请选择登录角色!", "登录验证");
                flag = false;
            }
            return flag;
        }
    }
}