简体   繁体   English

将登录的用户信息传递给另一种形式的C#MySQL

[英]Passing logged in user information to another form c# mysql

For example I want to display the logged in user's information from database to another form like firstname and lastname I got no problem in admin because it only has one form or details for it. 例如,我想将登录的用户信息从数据库显示为另一种形式,例如名和姓,我在admin中没有问题,因为它只有一种形式或详细信息。 I tried passing the text in textbox username to another form and then select the passed text to display the other information of it but it won't, here's my code btw. 我尝试将文本框用户名中的文本传递给另一种形式,然后选择传递的文本以显示它的其他信息,但是不会,这是我的代码btw。

private void button2_Click(object sender, EventArgs e)
    {
        int i = 0;
        MySqlCommand comm = con.CreateCommand();
        comm.CommandText = "select * from accountinfo where username = @user and pass = @password";
        comm.Parameters.AddWithValue("@user", textBox1.Text);
        comm.Parameters.AddWithValue("@password", textBox2.Text);
        MySqlDataReader myReader;
        con.Open();
        comm.ExecuteNonQuery();
        myReader = comm.ExecuteReader();
        string accountType = string.Empty;
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(comm);
        i = Convert.ToInt32(dt.Rows.Count.ToString());

       while (myReader.Read())
       {
            i = i + 1;
            accountType = myReader["accountType"].ToString();
       }
       if (i == 0)
       {
            MessageBox.Show("Wrong username or password!");
       }
       else if (accountType == "admin")
       {
            MessageBox.Show("Welcome admin");
            this.Hide();
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            Form3 frm3 = new Form3();
            frm3.Show();
       }
       else
       {
            MessageBox.Show("Welcome");
            this.Hide();
           using(var frm4 = new Form4())
           {

               frm4.FirstName = textBox1.Text;
               frm4.ShowDialog();
           }
       }
            con.Close();
    }

and my code in second form 和我的第二种形式的代码

public partial class Form4 : Form
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string CellNo { get; set; }

    public Form4()
    {
        InitializeComponent();
    }

    private void Form4_Load(object sender, EventArgs e)
    {
        tbUser.Text = FirstName;
        try
        {
            string MyConnection2 = "server=localhost;user id=root;database=account;persistsecurityinfo=True;PASSWORD=test123;SslMode=none";
            string Query = "SELECT firstname = '" + tbFN.Text + "' from accountinfo WHERE username = '" + tbUser + "' " ; 
            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
            MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
            MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
            MyAdapter.SelectCommand = MyCommand2;
            DataTable dTable = new DataTable();
            MyAdapter.Fill(dTable);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        } 

    }
}

Logged in user information is kind of static for all your application, so you should be able to access to it from anywhere 对于所有应用程序而言,登录的用户信息都是静态的,因此您应该可以从任何地方访问它

As a solution create class user 作为解决方案创建类用户

public class User {
    username, full name ...
}

Create embiend class ApplicationContext 创建嵌入类ApplicationContext

public class ApplicationContext
{
   private ApplicationContext(){
   }

   public User UserInfo {get;}

   public static ApplicationContext Current{get;}

   public static Init(User userInfo)
   {
       if(Current != null)
           throw new Exception("Context already initialized");

       Current = new ApplicationContext(){
           UserInfo = userInfo
       }
   }
}

After login call 登录后通话

ApplicationContext.Init(userInfo);

Everywhere where you need user info call 您需要致电用户信息的任何地方

ApplicationContext.Current.UserInfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM