gpt4 book ai didi

C# MySql 将数据库中特定行的参数从一种表单传递到另一种表单

转载 作者:行者123 更新时间:2023-11-29 21:57:04 25 4
gpt4 key购买 nike

大家好,我想寻求帮助我创建了一个连接到 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;*/
}
}
}

最佳答案

而不是这样做:

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

尝试这样做:

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

请注意,在后一个代码段中,您将在设置相应属性后显示表单。

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

关于C# MySql 将数据库中特定行的参数从一种表单传递到另一种表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com