C#初学者做一个简单的用户登陆界面用Access数据库后续来了

上次的后续来了

Form1画面设计以及代码如下

在这里插入图片描述

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.OleDb;//用access数据库连接时需添加这句

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {          
            //1.创建路径及数据库名
            //string strPath = Application.StartupPath + "\\dengLu.accdb";
            //2.生成链接数据库字符串
            //string oleCon = "provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:DataBase Password='xxx123';User Id='admin';Data source=" + strPath;
            string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\dengLu.accdb";
            //3.创建数据库连接
            OleDbConnection conn = new OleDbConnection(conStr);
            //4.当创建好连接到Access后打开数据库连接
            conn.Open();
            //5.SQL查询语句
            string Access = "select * from yonghu where UserName='" + this.textBox1.Text + "'";//select是查询数据库语句
            string Access1 = "select * from yonghu where UserName='" + this.textBox1.Text + "'and PassWord='" + this.textBox2.Text + "'";//select是查询数据库语句
            //6.新声明一个OleDbCommand对象
            OleDbCommand cmd = new OleDbCommand(Access, conn);           
            //7.从数据库读出来的数据流赋给read,简而言之就是读取数据库中实时数据流
            OleDbDataReader read = cmd.ExecuteReader();

            if (read.Read() == true) {
                OleDbCommand cmd1 = new OleDbCommand(Access1, conn);
                OleDbDataReader read1 = cmd1.ExecuteReader();

                if (read1.Read() == true)
                {
                    string passData = textBox1.Text;
                    Form2 newForm2 = new Form2(passData);
                    newForm2.Show();//到Form2
                        Hide();//隐藏此窗口
                }
                else
                {
                    MessageBox.Show("密码错误!");
                }
            }
            else
            {
                    MessageBox.Show("账户错误!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();//关闭整个程序
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form3 newForm3 = new Form3();
            newForm3.Show();//到Form2
            Hide();//隐藏此窗口
        }

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

Form2画面设计以及代码如下

在这里插入图片描述

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.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        string ReceiveData;//一个字段,保存数据
        public Form2(string ReceiveStr)//修改构造函数,增加一个字符串形参
        {
            InitializeComponent();
            ReceiveData = ReceiveStr;
            label2.Text = "登陆成功!";
            label3.Text = ReceiveStr;
            label4.Text = "恭喜用户:";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 newForm1 = new Form1();
            newForm1.Show();//到Form1
            Hide();//隐藏此窗口
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {            
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void label2_Click(object sender, EventArgs e)
        {
        }
        private void label3_Click(object sender, EventArgs e)
        {
        }
        private void label4_Click(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //string b = Helper.Name;//刚才form1中收到用户名的值,这里可以直接调用并将其赋值,这样就知道是删除的哪个账户了
            Form4 newForm4 = new Form4(ReceiveData);
            newForm4.Show();//到Form2
            Hide();//隐藏此窗口
        }
        private void button3_Click(object sender, EventArgs e)
        {
            Form5 newForm5 = new Form5(ReceiveData);
            newForm5.Show();//到Form5
            Hide();//隐藏此窗口
        }
        private void Form2_Load(object sender, EventArgs e)
        {
        }
        private void label3_Click_1(object sender, EventArgs e)
        {
        }
    }
}

Form3画面设计以及代码如下

在这里插入图片描述

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.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            else
            {
                if (textBox2.Text == "")
                {
                    MessageBox.Show("密码不能为空!");
                }
                else
                {
                    if (textBox3.Text == "")
                    {
                        MessageBox.Show("确认密码不能为空!");
                    }
                    else
                    {
                        if (textBox2.Text != textBox3.Text)
                        {
                            MessageBox.Show("两次输入的密码不同!");
                        }
                            else
                            {
                            string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Pc\\Desktop\\WindowsFormsApplication1\\WindowsFormsApplication1\\bin\\Debug\\dengLu.accdb";
                            OleDbConnection conn = new OleDbConnection(conStr);
                            conn.Open();
                            string Access = "select * from yonghu where UserName='" + this.textBox1.Text + "'";//select是查询数据库语句
                            string insert = "insert into [yonghu] ([UserName],[PassWord]) values('" + textBox1.Text + "','" + textBox2.Text + "')";
                            OleDbCommand cmd = new OleDbCommand(Access, conn);
                            OleDbDataReader read = cmd.ExecuteReader();
                            if (read.Read() == true)
                            {
                                MessageBox.Show("用户名已存在");
                            }
                            else
                            {
                                OleDbCommand myComm = new OleDbCommand(insert, conn);
                                myComm.ExecuteNonQuery();
                                MessageBox.Show("注册成功!");
                            }
                           }
                        }
                    }
                }
            }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 newForm1 = new Form1();
            newForm1.Show();//到Form2
            Hide();//隐藏此窗口
        }
        private void Form3_Load(object sender, EventArgs e)
        {
        }
    }
 }

Form4画面设计以及代码如下

在这里插入图片描述

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.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        string ReceiveData;//一个字段,保存数据
        public Form4(string ReceiveStr)
        {
            InitializeComponent();
            ReceiveData = ReceiveStr;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 newForm2 = new Form2(ReceiveData);
            newForm2.Show();//到Form2
            Hide();//隐藏此窗口
        }
        private void button1_Click(object sender, EventArgs e)
        {            
            if (this.textBox1.Text == this.textBox2.Text)
            {            
                string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Pc\\Desktop\\WindowsFormsApplication1\\WindowsFormsApplication1\\bin\\Debug\\dengLu.accdb";
                OleDbConnection conn = new OleDbConnection(conStr);
                conn.Open();
                string modify = "update [yonghu] set [PassWord]  = '" + this.textBox1.Text + "' where [UserName] ='"+ ReceiveData + "'";
                OleDbCommand cmd = new OleDbCommand(modify, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("两次输入的密码不同!");
            }
        }
        private void Form4_Load(object sender, EventArgs e)
        {
        }
    }
}

Form5画面设计以及代码如下

在这里插入图片描述

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.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        string ReceiveData;//一个字段,保存数据
        public Form5(string ReceiveStr)
        {
            InitializeComponent();
            label2.Text = ReceiveStr;
            ReceiveData = ReceiveStr;
        }
        private void button1_Click(object sender, EventArgs e)
        {              
            string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Pc\\Desktop\\WindowsFormsApplication1\\WindowsFormsApplication1\\bin\\Debug\\dengLu.accdb";
            OleDbConnection conn = new OleDbConnection(conStr);
            conn.Open();
            String Delete = "delete from [yonghu] where [UserName] ='" + ReceiveData + "'";
            OleDbCommand cmd = new OleDbCommand(Delete, conn);
            cmd.ExecuteNonQuery();
            MessageBox.Show("用户删除成功!");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form1 newForm1 = new Form1();
            newForm1.Show();//到Form2
            Hide();//隐藏此窗口
        }
    }
}

数据库内容详情如下

在这里插入图片描述

注意事项:

1.Data Source=C:\Users\Pc\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\dengLu.accdb"这个路径是在电脑桌面的,如果你的路径与我的路径不同,请自行修改,否则会报错

2.delete from [yonghu] where [UserName]这里在删除数据的时候加了[],因为如果不加的话,会出错,可能是因为版本原因或是其他,具体原因不太了解,我同学的不添加[]也能运行,但我的不行,加不加请自行判断

3.可能会有一些多余的按钮显示,是因为当时在实验,删了后又添加新的,你可以删除,但出不出错我就不确定了

4.以上代码直接写入,是可以运行成功的。但有点瑕疵,比如:注册完账户后,他不会自己退出到登陆界面,需要自己动手点击退出按钮,才会返回。

请大神嘴下留情:

就是给大家提供一个思路,本人接触C#时间不长,里面体能会有很多考虑不周,类似于内存占用过多等等,还请大神误喷

总结:这是对之前项目的完善,如果对你有帮助,那就太好了。

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值