簡體   English   中英

將登錄的用戶信息傳遞給另一種形式的C#MySQL

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

例如,我想將登錄的用戶信息從數據庫顯示為另一種形式,例如名和姓,我在admin中沒有問題,因為它只有一種形式或詳細信息。 我嘗試將文本框用戶名中的文本傳遞給另一種形式,然后選擇傳遞的文本以顯示它的其他信息,但是不會,這是我的代碼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();
    }

和我的第二種形式的代碼

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);
        } 

    }
}

對於所有應用程序而言,登錄的用戶信息都是靜態的,因此您應該可以從任何地方訪問它

作為解決方案創建類用戶

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

創建嵌入類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
       }
   }
}

登錄后通話

ApplicationContext.Init(userInfo);

您需要致電用戶信息的任何地方

ApplicationContext.Current.UserInfo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM