关于VS2019C#如何建立登陆界面

输入的用户名和密码需与Access数据库的记录相匹配,成功才能显示主界面,麻烦详细一点

题主要的代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~如需要工程文件请站内发邮箱

img

Form1.cs,登录界面

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

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtUN.Text.Trim() == "" || txtPwd.Text.Trim() == "") { MessageBox.Show("账号或密码不能为空!");return; }
            string sql = "select id from [users] where un='" + txtUN.Text.Replace("'", "''") + "' and pwd='" + txtPwd.Text.Replace("'","''")+"'";
            OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=data.mdb");
            conn.Open();
            object id = new OleDbCommand(sql, conn).ExecuteScalar();
            conn.Close();
            if (id == null) MessageBox.Show("账号或密码错误!");
            else
            {
                this.Hide();
                var m = new Form2() { id = (int)id, loginFrm = this , name = txtUN.Text };//显示主窗体,主窗体有3个属性,一个存储用户id,方便读取数据用,第二个存储登录窗体,第三个存储用户名
                m.Show();
            }
        }
    }
}


Form2.cs,主界面

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 WindowsFormsApp3
{
    public partial class Form2 : Form
    {
        public Form1 loginFrm { get; set; }
        public int id { get; set; }
        public string name { get; set; }
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = "用户Id:" + id + "  账号名:"+name;
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            loginFrm.Show();
        }
    }
}