c# MySql将数据库中特定行的参数从一种形式传递到另一种形式

本文关键字:一种 另一种 数据库 MySql 参数 | 更新日期: 2023-09-27 18:17:00

大家好,我想寻求帮助我创建了一个简单的注册系统,它连接到mysql数据库

我有一个表单,你将创建一个简单的个人信息与用户名和密码,所以在你创建它将保存在我的数据库

所有我想要的是,当我现在将登录的用户名和密码集会弹出一个新表单,那里有4个标签。这4个标签应该变成我创建的用户名和密码集的个人详细信息

那么我如何将它传递到另一个表单,我如何将级别更改为用户名的具体细节

这是我的代码FORM1(登录表单):
private void pictureBox2_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        if (comboBox1.Text == "ADMIN")
        {
            if (textBox1.Text.Equals("ADMIN") && textBox2.Text.Equals("PASSWORD"))
            {
                MessageBox.Show("LOGIN SUCCESS");
                this.Hide();
                frm2.Show();
            }
            else
            {
                MessageBox.Show("INCORRECT USERNAME OR PASSWORD!");
            }
        }
        else if (comboBox1.Text=="USER"){
            MySqlConnection connection = new MySqlConnection(MyConnectionString);
            //MySqlCommand cmd;
            //connection.Open();

            string selectString =
                                    "SELECT username, password " +
                "FROM enrollment_system " +
                "WHERE username = '" + textBox1.Text + "' AND password = '" + textBox2.Text + "'";
                MySqlCommand mySqlCommand = new MySqlCommand(selectString, connection);
                connection.Open();
                String strResult = String.Empty;
                strResult = (String)mySqlCommand.ExecuteScalar();
                connection.Close();
                try
                {
                    if (strResult.Length == 1)
                    {
                        MessageBox.Show("FAIL TO LOGIN");
                        //could redirect to register page
                    }
                    else
                    {
                        MessageBox.Show("GOOD TO LOGIN"+samples);
                        this.Hide();
                        Form6 frm6 = new Form6(this);
                        frm6.Show();
                        frm6.studentid = textBox1.Text;
                    }
                }
                catch
                {
                    MessageBox.Show("Fail to login");
                }
        }
        else
        {
            MessageBox.Show("SELECT ACCOUNT TYPE");
        }
    }

Form 6( the form that will pop out and represents as the student info)
    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication17
{
    public partial class Form6 : Form
    {
        Form1 AccountForm { get; set; }
        public string studentid;
        public string firstname;
        public string lastname;
        public string middle;
        public string course;
        public string yearlevel;
        public string type;
        public Form6(Form1 _form1)//
        {
            AccountForm = _form1;
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_NCHITTEST)
                m.Result = (IntPtr)(HT_CAPTION);
        }
        private const int WM_NCHITTEST = 0x84;
        private const int HT_CLIENT = 0x1;
        private const int HT_CAPTION = 0x2;
        private void Form6_Load(object sender, EventArgs e)
        {
            labelStudent.Text = studentid;
            labelCourse.Text = course;
            /*
            labelYear.Text = yearlevel;
            labelType.Text = type;*/
        }
      }
    }

c# MySql将数据库中特定行的参数从一种形式传递到另一种形式

而不是:

Form6 frm6 = new Form6(this);
frm6.Show();
frm6.studentid = textBox1.Text;

尝试:

Form6 frm6 = new Form6(this);
frm6.studentid = textBox1.Text;
frm6.Show();
注意,在后面的代码片段中,在设置相应的属性后显示了形式。

在前一种方法中,修改已经显示的表单上的控件需要您使其无效(重新绘制),以便实际看到更改。