数据库 第一章Windows程序

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        //
        public Form1()
        {
           
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("确定取消登陆吗");
            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 ADMIN = "管理员";

        private void button1_Click(object sender, EventArgs e)
        {
            //用户名 密码和用户类型都不为空
            if (CheckInput())
            {
                if (this.comboBox1.Text.Equals(ADMIN))
                {
                    guanliyuan d = new guanliyuan();
                    d.name = textBox1.Text;
                    d.Show();
                }
                this.Hide();
            }
        }
        //用户名为空
        public bool CheckInput()
        {
            if (this.textBox1.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入用户名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.label1.Focus();
                return false;
            }
                //密码为空
            else if (this.textBox2.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtpwd.Focus();
                return false;
            }
                //用户类型为空
            else if (this.comboBox1.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请选择登录类型", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.cboLoginType.Focus();
                return false;
            }
            else
            {
                guanliyuan d = new guanliyuan();
                d.name = textBox1.Text;
                return true;
            }
        }
         public bool CheckUser(ref string message)
        {
            bool i = false;
            string name = this.textBox1.Text.Trim();
            string mima = textBox2.Text.Trim();
            StringBuilder sb = new StringBuilder();
            if (txtpwd.Equals(ADMIN))
            {
                sb.AppendFormat("SELECT COUNT(*) FROM [Table_1]" + " WHERE [name]= '" + name + "' AND [mima]='" + mima + "'");
            }
            int count = 0;
            DEHelper dbhelper = new DEHelper();
            try
            {
                SqlCommand comm = new SqlCommand(sb.ToString(), dbhelper.Connection);
                dbhelper.OpenConnection();
                count = (int)comm.ExecuteScalar();
                if (count > 0)
                {
                    i = true;
                }
                else
                {
                    message = "用户名或密码不存在";
                    i = false;
                }
            }
            catch (Exception)
            {
                message = "系统发生错误,请稍后再试";
                i = false;
            }
            finally
            {
                dbhelper.CloseConnection();
            }
            return i;
        }

        }

       
    }

    

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

namespace WindowsFormsApplication2
{
    class DEHelper
    {
        private string connString = "Data Source=.;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();
            }
        }
    }
}

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 WindowsFormsApplication2
{
    public partial class guanliyuan : Form
    {
        public  string name = "";
        public guanliyuan()
        {
            InitializeComponent();
        }

        private void guanliyuan_Load(object sender, EventArgs e)
        {
            this.aa.Text = name;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值