⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.cs

📁 登陆界面的权限与连接数据库 用C#编写的
💻 CS
字号:
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.Odbc;

namespace ming
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox6.UseSystemPasswordChar = true;
            textBox5.UseSystemPasswordChar = true;
            textBox2.UseSystemPasswordChar = true;
            textBox3.UseSystemPasswordChar = true;
            string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +//也是comboBox的绑定
                                "SERVER=localhost;" +
                                "DATABASE=user_imformation;" +
                                "UID=root;" +
                                "PASSWORD=123;" +
                                "OPTION=3";
            OdbcConnection conn = new OdbcConnection(MyConString);
            conn.Open();
            OdbcCommand mycommand = conn.CreateCommand();
            mycommand.CommandText = "select * from yg";
            OdbcDataAdapter adapter = new OdbcDataAdapter(mycommand);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset, "yg");
            DataTable datatable = dataset.Tables["yg"];
            comboBox1.DataSource = datatable.DefaultView;
            comboBox1.DisplayMember = datatable.Columns["name"].ColumnName;
            comboBox1.ValueMember = datatable.Columns["id"].ColumnName;
        }

        private  void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空!");
                textBox1.Select();
            }
            else if (comboBox1.Text == "")
            {
                MessageBox.Show("用户类型不能为空!");
                comboBox1.Select();
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("密码不能为空!");
                textBox2.Select();
            }
            else if (textBox3.Text == "")
            {
                MessageBox.Show("确认密码不能为空!");
                textBox3.Select();
            }
            else if (textBox3.Text != textBox2.Text)
            {
                MessageBox.Show("两次输入密码不一致,请重新输入!");

            }
            else
            {
                string username = this.textBox1.Text.ToString();//插入数据库
                string pwd = this.textBox2.Text.ToString();
                string ze = this.comboBox1.Text.ToString();

                string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
                                        "SERVER=localhost;" +
                                        "DATABASE=user_imformation;" +
                                        "UID=root;" +
                                        "PASSWORD=123;" +
                                        "OPTION=3";

                OdbcConnection MyConnection = new OdbcConnection(MyConString);

                OdbcCommand MyCommand = MyConnection.CreateCommand();
                MyConnection.Open();
                MyCommand.CommandText = "INSERT INTO user_imformation(username,password,userid,psw) VALUES ('" + username + "', '" + pwd + "', '" + ze + "','') ";
                string sql = "select * from user_imformation where username='" + textBox1.Text + "'";
                OdbcCommand co = new OdbcCommand(sql, MyConnection);
                int n = Convert.ToInt32(co.ExecuteScalar());//用户是否存在的检验
                if (n > 0)
                {

                    MessageBox.Show("该用户已经存在,请重新输入!");
                    this.textBox1.Text = "";
                    textBox1.Select();
                }
                else
                {
                    OdbcDataReader MyReader = MyCommand.ExecuteReader();
                    MessageBox.Show("添加成功");
                    MyConnection.Close();
                    Close();
                   
                    
                 
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox4.Text == "")
            {
                MessageBox.Show("用户名不能为空!");
                textBox4.Select();
            }
            else if (textBox5.Text == "")
            {
                MessageBox.Show("密码不能为空!");
                textBox5.Select();
            }
            else if (textBox6.Text == "")
            {
                MessageBox.Show("密码不能为空!");
                textBox6.Select();
            }
            else if (textBox5.Text == textBox6.Text)
            {
                MessageBox.Show("两次输入密码一致,请重新输入!");

            }
            else
            {
                string username = this.textBox4.Text.ToString();//修改密码,也就是更新数据库已有的数据
                string pwd = this.textBox5.Text.ToString();
                string pwd2 = this.textBox6.Text.ToString();
                string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
                                 "SERVER=localhost;" +
                                 "DATABASE=user_imformation;" +
                                 "UID=root;" +
                                 "PASSWORD=123;" +
                                 "OPTION=3";

                OdbcConnection MyConnection = new OdbcConnection(MyConString);
                MyConnection.Open();
                OdbcCommand MyCommand = MyConnection.CreateCommand();
                MyCommand.CommandText = "Select * from user_imformation where username='" + username + "'";
                OdbcDataReader MyReader = MyCommand.ExecuteReader();
                if (MyReader.Read())
                {
                    if (MyReader["password"].ToString().Trim() == pwd)
                    {

                        OdbcCommand cmd = new OdbcCommand("UPDATE user_imformation SET password = '" + pwd2 + "' where username='" + username + "'", MyConnection);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("修改成功");
                        Close();
                    }

                    else
                    {
                        MessageBox.Show("密码错误");
                    }
                }
                else
                {
                    MessageBox.Show("没有此用户");
                }
                MyConnection.Close();
                MyReader.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -