将数据保存在Form2中,帐户ID在1列中查看谁';他拥有这些数据

本文关键字:数据 拥有 1列 Form2 存在 保存 帐户 ID | 更新日期: 2023-09-27 18:25:27

理想情况下,我想要这样做,当用户登录并转到form2时。当他们写新数据并按save将数据保存在mysql数据库中时,他们的帐户ID在1列中,但我不知道如何做到这一点,所以我在这里寻求帮助。

Form1相关代码:

            MySqlConnection con = new MySqlConnection("server=SERVERIP;port=3306;database=DATABASE;uid=USERNAME;password=PASSWORD");
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM account WHERE username='" + this.user_txt.Text + "' AND pass='" + this.password_txt.Text + "';", con);
            MySqlDataReader MyReader;
            con.Open();
            MyReader = cmd.ExecuteReader();
            int count = 0;
            while (MyReader.Read())
            {
                count = count + 1;
            }
            if (count == 1)
            {
                this.Hide();
                var form1 = new Form2();
                form1.Show();
            }

Form2相关代码:

        using (MySqlConnection con = new MySqlConnection("server=SERVERIP;port=3306;database=DATABASE;uid=USERNAME;password=PASSWORD"))
        {
            con.Open();
           using (MySqlCommand cmd = new MySqlCommand("insert into info(Datum,Timmar,Rast) Values(@Datum,@Timmar,@Rast)", con))
            {
               DateTime now = DateTime.Now;
               cmd.Parameters.AddWithValue("@Datum", now);
               cmd.Parameters.AddWithValue("@Timmar", textBox2.Text);
               cmd.Parameters.AddWithValue("@Rast", textBox3.Text);
               cmd.ExecuteNonQuery();
               MessageBox.Show("Sparat!");
               con.Close();
            } 
        }

因此,我想做的是将带有form2中的信息的帐户id添加到数据库中。类似于:

            using (MySqlCommand cmd = new MySqlCommand("insert into info(AccountId,Datum,Timmar,Rast) Values(@AccountId,@Datum,@Timmar,@Rast)", con))
            {
               DateTime now = DateTime.Now;
               cmd.Parameters.AddWithValue("@AccountId", ACCOUNT_ID_SOURCE);
               cmd.Parameters.AddWithValue("@Datum", now);
               cmd.Parameters.AddWithValue("@Timmar", textBox2.Text);
               cmd.Parameters.AddWithValue("@Rast", textBox3.Text);
               cmd.ExecuteNonQuery();
               MessageBox.Show("Sparat!");
               con.Close();
            } 

现在我试着用它来获得用户输入,他们输入到form2,但没有成功。

               var frm1 = new Form1(); //Form2
               string ACCOUNT_ID_SOURCE = frm1.UserText; //Form2
               public string UserText { get; private set; } //form1
               UserText = this.user_txt.Text; //form1

关于如何解决这个问题有什么想法吗?

将数据保存在Form2中,帐户ID在1列中查看谁';他拥有这些数据

这个怎么样?

Sol 1。将userid设置为Form2的构造函数参数。

readonly string ACCOUNT_ID_SOURCE;
public Form2(string aUserID)
{
     InitializeComponent();
     ACCOUNT_ID_SOURCE = aUserID;
}

Sol 2。使用户信息成为静态类。

1) 类

public class UserInfo
{
    public static UserInfo instance = new UserInfo();
    public string LogInID { get; set; }
}

2) 表格1

UserInfo.instance.LogInID = "MyID";

3) 表格2

UserInfo.instance.LogInID; // Use this property

尝试创建一个新的类

 public class UserModel
   {
    public int IdUser { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
 }

然后在窗体1中的按钮点击事件

 private void button1_Click(object sender, EventArgs e)
    {
       UserModel userModel = new UserModel();
        using (SqlConnection conn = new SqlConnection(@"Data Source=localhost; Initial   Catalog=Northwind; Integrated Security=True"))
        {
        //here your code to retrieve data from database
        userModel.FirstName = "";
        userModel.LastName = "";
        userModel.IdUser = 1;
        }           

        Form2 frm2 = new Form2(userModel);
        this.Hide();
        frm2.Show();
    }

在你的表格2中

  public partial class Form2 : Form
{
    private UserModel UserObject { get; set; }
    public Form2()
    {
        InitializeComponent();
    }
    public Form2(UserModel userModel)
    {           
        InitializeComponent();
        this.UserObject = userModel;
    }
    private void btnSaveData_Click(object sender, EventArgs e)
    {
        using (MySqlCommand cmd = new MySqlCommand("insert into info(AccountId,Datum,Timmar,Rast) Values(@AccountId,@Datum,@Timmar,@Rast)", con))
        {
            DateTime now = DateTime.Now;
            cmd.Parameters.AddWithValue("@AccountId", UserObject.IdUser);
            cmd.Parameters.AddWithValue("@Datum", now);
            cmd.Parameters.AddWithValue("@Timmar", textBox2.Text);
            cmd.Parameters.AddWithValue("@Rast", textBox3.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Sparat!");
            con.Close();
        } 
    }
}