用C#来写一个管理系统的登录界面

来源:互联网 发布:mac os 10.10 iso镜像 编辑:程序博客网 时间:2024/04/29 06:07

登录系统的代码:

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

namespace 登录系统与注册
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SqlConnection conn;
        private void RegisterButton_Click(object sender, EventArgs e)
        {
            if (NameText.Text == "" || passwordText.Text == "")
            {
                MessageBox.Show("用户名或密码不能为空!!!");
                return;
            }
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from userTest", conn);
                SqlCommandBuilder bc = new SqlCommandBuilder(da);
                DataSet ds = new DataSet();
                da.Fill(ds, "userTest");
                DataTable dt = ds.Tables["userTest"];
                Object[] arr = new Object[2];
                foreach (DataRow row in dt.Rows)
                {
                    arr = row.ItemArray;
                    if (NameText.Text.Equals(arr[0].ToString()))
                    {
                        if (passwordText.Text.Equals(arr[1].ToString()))
                        {
                            MessageBox.Show("登录成功!!!");
                            return;
                        }
                        else
                        {
                            MessageBox.Show("密码不正确,请查证!!!");
                            passwordText.Text = "";
                            return;
                        }
                    }
                }
                MessageBox.Show("用户名不存在,请先注册!!!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("Data Source=localhost;Initial Catalog=users;Integrated Security=True");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            NameText.Text = "";
            passwordText.Text ="";
        }

        private void zhuButton_Click(object sender, EventArgs e)
        {
         
            zhu z = new zhu();
            this.Hide();
            z.ShowDialog();
         
        }
    }
}

 

 

 

 

 

 

注册的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 登录系统与注册
{
    public partial class zhu : Form
    {
        public zhu()
        {
            InitializeComponent();
        }

        private void zhuButton_Click(object sender, EventArgs e)
        {
            if (textPassword.Text == "" || textPassword2.Text == "" || textName.Text == "")
            {
                MessageBox.Show("注册信息不能为空!!!");
                return;
            }
            if (!textPassword2.Text.Equals(textPassword.Text))
            {
                MessageBox.Show("两次输入的密码必须一致!!!");
                textPassword.Text = "";
                textPassword2.Text = "";
                return;

 

            }
            //判断用户名是否已经被注册了
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from userTest",conn);
                SqlCommandBuilder bc = new SqlCommandBuilder(da);
                DataSet ds = new DataSet();
                da.Fill(ds, "userTest");
                Object[] arr = new Object[2];
                DataTable dt = ds.Tables["userTest"];
                foreach (DataRow row in dt.Rows)
                {
                    arr = row.ItemArray;
                    if (textName.Text.Equals(arr[0]))
                    {
                        MessageBox.Show("该用户已经被注册,请更换用户名!");
                        textName.Text = "";
                        textPassword.Text = "";
                        textPassword2.Text = "";
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }


            try
            {
                SqlCommand com = new SqlCommand("insert into userTest values('" + textName.Text + "','" + textPassword.Text + "')", conn);
                conn.Open();
                com.ExecuteNonQuery();
                MessageBox.Show("恭喜!注册成功!!!");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        private SqlConnection conn;
        private void zhu_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("Data Source=localhost;Initial Catalog=users;Integrated Security=True");

         
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            textName.Text = "";
            textPassword.Text = "";
            textPassword2.Text = "";

        }

        private void backButton_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            this.Hide();       
            f.ShowDialog();
        }
     
   
   
    }
}

原创粉丝点击