学习信息管理系统

class DBHelper
    {
        private string connString = @"Data Source=LAPTOP-ODTAIH4A;Initial Catalog=MySchool;Integrated Security=True";
        private SqlConnection connection;
        public SqlConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = new SqlConnection(connString);
                }
                return connection;
            }
        }
        public void OpenConnection()
        {
            if (Connection.State == ConnectionState.Closed)
            {

                connection.Open();

            }
            else if (Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
                Connection.Open();
            }
        }
        public void CloseConnection()
        {
            if (Connection.State == ConnectionState.Open || connection.State == ConnectionState.Broken)
            {

                connection.Close();


            }

        }
    }
}

public partial class Form1 : Form
    {
        public const string ADMIN = "系统管理员";
        public const string CAPTION = "输入提示";
        public Form1()
        {
            InitializeComponent();
        }
        public bool denglu(ref string message)
        {
            bool a = false;
            string userName = yonghuming.Text.Trim();
            string userPwd = mima.Text.Trim();
            StringBuilder sb = new StringBuilder();
            if (this.leixing.Text.Equals(ADMIN))
            {
                sb.AppendFormat("select count(*) from admin" + " where loginId='{0}' and loginpwd='{1}'", userName, userPwd);
            }
            int count = 0;
            DBHelper db = new DBHelper();

            try
            {
                SqlCommand comm = new SqlCommand(sb.ToString(),db.Connection);
                db.OpenConnection();
                count = (int)comm.ExecuteScalar();
                if (count > 0)
                {

                    a = true;

                }
                else
                {
                   
                    message = "用户名或密码不存在"; 
                    a = false;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
                a = false;
            }
            finally
            {
                db.CloseConnection();

            }

            return a;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }


        public bool CheckInput()
        {

            if (this.yonghuming.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入用户名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.yonghuming.Focus();
                return false;
            }
            else if (this.mima.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.yonghuming.Focus();
                return false;
            }
            else if (this.leixing.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入登陆类型", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.yonghuming.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btndenglu_Click(object sender, EventArgs e)
        {
            string message = string.Empty;
            if (CheckInput() && denglu(ref message))
            {
                if (this.leixing.Text.Equals(ADMIN))
                {
                    FrmAdminMain fraAdmin = new FrmAdminMain();
                    MessageBox.Show("登陆成功", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    fraAdmin.userId = "管理员主窗体";
                    fraAdmin.Show();

                }

            }
            else {

                MessageBox.Show("登陆失败", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
              
            
            }
        }

        private void btnquxiao_Click(object sender, EventArgs e)
        {
          DialogResult r= MessageBox.Show("确认退出?","取消",MessageBoxButtons.YesNo,MessageBoxIcon.Hand);
          if (r == DialogResult.Yes)
          {
              this.Close();

          }
          else {

              return;

          
          }
        }

 public partial class FrmAdminMain : Form
    { 
        public string userId=string.Empty;
        public FrmAdminMain()
        {
          
            InitializeComponent();
        }

        private void FrmAdminMain_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = userId;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值