第一章 所有上机练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace MySchool
{
    public class DBHelper
    {
        private string connString = @"Data Source=.;Initital Catalog = MySchool; User ID=sa;Pwd=bdqn";
        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();
            }
        }
    }
}

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

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

        private void FrmAdminMainin_Load(object sender, EventArgs e)
        {
            textBox1.Text = userId;           
        }
        public const string ADMIN = "系统管理员";
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                string message = string.Empty;
                if (CheckUser(ref message))
                {
                    if (this.cboLoginType.Text.Equals(ADMIN))
                    {
                        FrmAdminMainin frmAdmin = new FrmAdminMainin();
                        frmAdmin.Show();
                    }
                    this.Hide();
                }
            }
        }
        public bool CheckUser(ref string message)
        {
            bool isValidUser = false;
            string userName = textBox1.Text.Trim();
            string userPwd = textBox2.Text.Trim();
            StringBuilder sb = new StringBuilder();
            if (cboLoginType.Equals(ADMIN))
            {
                sb.AppendFormat("SELECT COUNT(*) FROM [Admin]" + " WHERE [LoginId] = '{0}' AND [LoginPwd]='{1}'", userName, userPwd);
            }
            int count = 0;
            DBHelper dbhelper = new DBHelper();
            try
            {
                SqlCommand command = new SqlCommand(sb.ToString(), dbhelper.Connection);
                dbhelper.OpenConnection();
                count = (int)command.ExecuteScalar();
                if (count > 0)
                {
                    isValidUser = true;
                }
                else
                {
                    message = "用户名或密码不存在!";
                    isValidUser = false;
                }
            }
            catch (Exeception ex)
            {
                message = "系统发生错误,请稍后再试!";
                isValidUser = false;
            }
            finally
            {
                dbhelper.CloseConnection();
            }
            return isValidUser;
        }
    }
}

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

namespace MySchool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.BackColor == Color.Red)
            {
                this.BackColor = Color.Yellow;
            }
            else if (this.BackColor == Color.Yellow)
            {
                this.BackColor = Color.Green;
            }
            else
            {
                this.BackColor = Color.Red;
            }
        }
        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }
        public const string CAPTION = "输入提示";
        private void denglu_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {                
            }
        }
        public bool CheckInput()
        {
            if (this.textBox1.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入用户名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox1.Focus();
                return false;
            }
            else if (this.textBox2.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox2.Focus();
                return false;
            }
            else if (this.comboBox1.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.comboBox1.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }
        private void quxiao_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认取消登录吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                this.Close();
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

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

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

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值