Visual Studio 2022 编写学校收费管理系统,后端数据库为Access


LoginForm页面代码
using SchoolFeeManagementSystem;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp7
{
    public partial class LoginForm : Form
    {
        private string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:新建文件夹\WindowsFormsApp7\WindowsFormsApp7\学校收费管理系统1.accdb";
        private OleDbConnection connection;
        public LoginForm()
        {
            InitializeComponent();
            connection = new OleDbConnection(connectionString);
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text.Trim();
            bool isAuthenticated = ValidateUser(username, password);

            if (isAuthenticated)
            {
                NavigationForm navigationForm = new NavigationForm(username, password);
                this.Hide();
                navigationForm.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool ValidateUser(string username, string password)
        {
            bool isAuthenticated = false;

            try
            {
                connection.Open();

                string query = "SELECT COUNT(*) FROM 账号表 WHERE 账号 = @username AND 密码 = @password";

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@username", username);
                    command.Parameters.AddWithValue("@password", password);

                    int count = (int)command.ExecuteScalar();

                    if (count > 0)
                    {
                        isAuthenticated = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库连接错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return isAuthenticated;
        }
    }
}
系统导航页面代码
using System;
using System.Windows.Forms;

namespace SchoolFeeManagementSystem
{
    public partial class NavigationForm : Form
    {
        private string username;
        private string password;

        public NavigationForm(string username, string password)
        {
            InitializeComponent();
            this.username = username;
            this.password = password;
        }

        private void btnPersonalInfo_Click(object sender, EventArgs e)
        {
            PersonalInfoForm personalInfoForm = new PersonalInfoForm(username, password);
            personalInfoForm.ShowDialog();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

个人信息管理页面代码
using System;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;

namespace SchoolFeeManagementSystem
{
    public partial class PersonalInfoForm : Form
    {
      
        private string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:新建文件夹\WindowsFormsApp7\WindowsFormsApp7\学校收费管理系统1.accdb";

       
        private string username = "";
        private string password = "";

        public PersonalInfoForm(string username, string password)
        {
            InitializeComponent();
            this.username = username;
            this.password = password;
        }

        private void PersonalInfoForm_Load(object sender, EventArgs e)
        {
      
            QueryPersonalInfo();
        }

        private void QueryPersonalInfo()
        {
            string studentId = txtStudentId.Text.Trim();

            try
            {
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {
                    connection.Open();

                  
                    string query = "SELECT * FROM 学生信息表 WHERE 学号 = @studentId AND 账号 = @username AND 密码 = @password";

                    using (OleDbCommand command = new OleDbCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@studentId", studentId);
                        command.Parameters.AddWithValue("@username", username);
                        command.Parameters.AddWithValue("@password", password);

                        OleDbDataAdapter adapter = new OleDbDataAdapter(command);

                      
                        DataTable dataTable = new DataTable();

                       
                        adapter.Fill(dataTable);

                        
                        dataGridView1.DataSource = dataTable;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库连接错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void PersonalInfoForm_Load_1(object sender, EventArgs e)
        {
            this.学生信息查询TableAdapter1.Fill(this.学校收费管理系统1DataSet1.学生信息查询);

        }
    }
}

这是我的程序流程图,但是在VisualStudio2022编写代码的时候我只设计了登录页面系统导航页面还有个人管理页面的相关操作代码
运行之后在登录页面输入正确账号密码点击登录按钮,无法跳转到系统导航页面,反而会显示用户名或密码错误的文本框
到底是连接数据库出了问题,还是代码无法正确读取数据库中的信息。
在点击运行之后,发现数据库连接会自动关闭,是与程序有关,还是别的问题

```

这个不好说,你有 try catch,可以在 catch 里下断点,看看具体什么错误。
也可以远程帮你看看。

可以嘗試 把 SELECT 的結果 用 MessageBox 或者 log file 來記錄並檢查結果。

【以下回答由 GPT 生成】

首先,你需要在Visual Studio 2022中创建一个新的Windows Forms应用程序项目。然后,你可以按照以下步骤编写学校收费管理系统,并将后端数据库设置为Access:

步骤 1: 创建数据库 - 在Visual Studio 2022中的“解决方案资源管理器”中,右键单击项目,选择“添加”->“新建项”。 - 在“添加新建项”对话框中,选择“数据”->“数据库”,并给数据库文件取一个名字,比如"学校收费管理系统.accdb"。 - 点击“添加”按钮,会自动在项目中创建一个新的Access数据库文件。

步骤 2: 创建表格 - 打开创建好的Access数据库文件。 - 在左侧的导航栏中,选择“表格”。 - 点击“设计”按钮,开始设计数据表结构。你可以根据学校收费管理系统的需求,创建适当的表格和字段。比如,创建一个名为"账号表"的表格,包含"账号"和"密码"字段。 - 保存并关闭Access数据库文件。

步骤 3: 在Visual Studio 中连接数据库 - 打开LoginForm.cs文件。在文件的顶部,添加一个引用命名空间:using System.Data.OleDb; - 在LoginForm类中声明一个私有的OleDbConnection对象和一个字符串类型的连接字符串变量。例如:

private OleDbConnection connection;
private string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=学校收费管理系统.accdb";
  • 在LoginForm类的构造函数中,初始化connection对象,例如:
public LoginForm()
{
   InitializeComponent();
   connection = new OleDbConnection(connectionString);
}

步骤 4: 编写验证用户的方法 - 在LoginForm类中添加一个私有的方法ValidateUser,用于验证用户输入的用户名和密码。例如:

private bool ValidateUser(string username, string password)
{
   bool isAuthenticated = false;

   try
   {
      connection.Open();

      string query = "SELECT COUNT(*) FROM 账号表 WHERE 账号 = @username AND 密码 = @password";

      using (OleDbCommand command = new OleDbCommand(query, connection))
      {
         command.Parameters.AddWithValue("@username", username);
         command.Parameters.AddWithValue("@password", password);

         isAuthenticated = (int)command.ExecuteScalar() > 0;
      }

      connection.Close();
   }
   catch (Exception ex)
   {
      MessageBox.Show("验证用户时出错:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }

   return isAuthenticated;
}

步骤 5: 编写登录按钮的点击事件处理方法 - 将你原来的btnLogin_Click方法更改为以下代码:

private void btnLogin_Click(object sender, EventArgs e)
{
   string username = txtUsername.Text.Trim();
   string password = txtPassword.Text.Trim();
   bool isAuthenticated = ValidateUser(username, password);

   if (isAuthenticated)
   {
      NavigationForm navigationForm = new NavigationForm(username, password);
      this.Hide();
      navigationForm.Show();
   }
   else
   {
      MessageBox.Show("用户名或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
}

步骤 6: 编译和运行 - 点击Visual Studio 2022工具栏中的"本地计算机"按钮进行编译和运行。 - 输入用户名和密码,点击登录按钮进行验证。

请注意:上述代码假设你的数据库文件名为"学校收费管理系统.accdb",你可以根据你自己的需要修改连接字符串中的文件名。如果你的数据库文件不在项目的根目录下,你需要提供完整的文件路径。

这是使用Visual Studio 2022编写学校收费管理系统并将后端数据库设置为Access的基本步骤和代码示例。希望对你有帮助!如果有任何进一步的疑问,请随时向我提问。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

你不是catch错误了吗,看看具体错误信息啊