C#开发数据库第一章上机题

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;

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

        private void AdminFrom_Load(object sender, EventArgs e)
        {
            textName.Text = user;
        }

    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace MySchool
{
    class DBHelper
    {
        private string strConn = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

        private SqlConnection conn;
        public SqlConnection conntection
        {
            get
            {
                if (conn == null)
                {
                    conn = new SqlConnection(strConn);
                }
                return conn;
            }
        }

        public void OpenConnection()
        {
            if (conntection.State == ConnectionState.Closed)
            {
                conntection.Open();
            }
            else if(conntection.State == ConnectionState.Broken)
            {
                conntection.Close();
                conntection.Open();
            }
        }


        public void CloseConnection()
        {
            if (conntection.State == ConnectionState.Open || conntection.State == ConnectionState.Broken)
            {
                conntection.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 Form1 : Form
    {
        


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定取消登录码", "操作显示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void Form1_Click(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;
            }
        }


        public const string CAPTION = "输入提示";                                                                                                                                                                          
        public const string ANDMIN= "管理员";

        private void button2_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                string message = string.Empty;

                if (CheckUser(ref message))
                {
                    
                    if (this.cboLoginType.Text.Equals(ANDMIN))
                    {
                        AdminFrom a = new AdminFrom();
                        a.user = textUserName.Text.Trim();
                        a.Show();
                    }
                    this.Hide();
                }
            }
        }

        public bool CheckInput()
        {
            if (this.textUserName.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入用户名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textUserName.Focus();
                return false;
            }
            else if (this.textPwd.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textPwd.Focus();
                return false;
            }
            else if (this.cboLoginType.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请选择数据类型", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.cboLoginType.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }


        public bool CheckUser(ref string message)
        {
            bool i = false;
            string userName = textUserName.Text.Trim();
            string userPwd = textPwd.Text.Trim();

            StringBuilder sb = new StringBuilder();
            if (cboLoginType.Text.Equals(ANDMIN))
            {
                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.conntection);
                db.OpenConnection();
                count = (int)comm.ExecuteScalar();
                if (count > 0)
                {
                    i = true;
                }
                else
                {
                    MessageBox.Show( "用户或密码不正确");
                    i = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                 MessageBox.Show( "系统发生错误,请稍后重试");
                i = false;
            }
            finally
            {
                db.CloseConnection();
            }
            return i;
        }



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值