如何在列表框中显示已记录的用户c#

时间:2014-03-06 19:22:34

标签: c# mysql

所以我有一个登录系统,当用户登录时,会打开一个新表单,我希望表单显示用户名。当用户注册时,他必须输入他的名字和姓氏,并且我希望在打开的新表单中显示这两件事。那么有没有简单的方法将登录的用户名放入列表框?

所以这是注册码:

MySqlConnection dataConnection = new MySqlConnection();
        dataConnection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
        dataConnection.Open();
        MySqlTransaction transakcija = dataConnection.BeginTransaction();
        MySqlCommand dataCommand = new MySqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.Transaction = transakcija;
        try
        {
            dataCommand.CommandText = "Insert INTO login.users (ime,upIme,geslo,dovoljenja) VALUES ('" + this.tB_Ime.Text + "','" + this.tB_upIme.Text + "','" + this.tB_geslo.Text + "', 'Navaden uporabnik')";
            dataCommand.CommandType = CommandType.Text;
            dataCommand.ExecuteNonQuery();
            transakcija.Commit();
            MessageBox.Show("Registracija uspešna!");
            this.Hide();
        }
        catch (Exception eks)
        {
            transakcija.Rollback();
            MessageBox.Show("Napaka pri registraciji\n" + eks.Message);
        }
        finally
        {
            dataCommand.Connection.Close();
        }
        Form1 f1 = new Form1();
        f1.ShowDialog();
        this.Close();

并登录:

try
        {
            string myConnection = "datasource=localhost;port=3306;username=root;password=";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            MySqlCommand SelectCommand = new MySqlCommand(" select * from login.users where upIme='" + this.tB_upIme.Text + "' AND geslo='" + this.tB_geslo.Text + "' ;", myConn);

            MySqlDataReader myReader;
            myConn.Open();
            myReader = SelectCommand.ExecuteReader();
            int count = 0;
            bool IsAdminUser = false;
            while (myReader.Read())
            {
                count = count + 1;
                IsAdminUser = myReader["dovoljenja"].Equals("Admin");
            }
            if (count == 1 && IsAdminUser == true)
            {
                MessageBox.Show("Prijavljeni ste kot administrator!");
                this.Hide();
                Form4 f4 = new Form4();
                f4.ShowDialog();
            }
            else if (count == 1)
            {
                MessageBox.Show("Uspešno ste se prijavili!");
                this.Hide();
                Form3 f3 = new Form3();
                f3.ShowDialog();
            }
            else if (count > 1)
            {
                MessageBox.Show("Dvojno uporabniško ime in geslo!");
                this.Hide();
            }
            else
                MessageBox.Show("Uporabniško ime ali geslo ni pravilno!");
            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

1 个答案:

答案 0 :(得分:0)

您可以将LoggedIn UserName发送到UserForm构造函数,然后可以将其添加到所需的控件中。

试试这个:

登录表单

        string LoggedInUserName = String.Empty;
        while (myReader.Read())
        {
            count = count + 1;
            IsAdminUser = myReader["dovoljenja"].Equals("Admin");
            LoggedInUserName = myReader["FirstName"].ToString()+
                               myReader["LastName"].ToString();
        }
        if (count == 1 && IsAdminUser == true)
        {
            MessageBox.Show("Prijavljeni ste kot administrator!");
            this.Hide();
            Form4 f4 = new Form4(LoggedInUserName);
            f4.ShowDialog();
        }
        else if (count == 1)
        {
            MessageBox.Show("Uspešno ste se prijavili!");
            this.Hide();
            Form3 f3 = new Form3(LoggedInUserName);
            f3.ShowDialog();
        }

现在更改管理表单(Form4)和UserForm(Form3)

    //now change the Admin Form Form4 constructor to take 1 argument
    public Form4(string username)
    {
        InitializeComponent();
        myListBox.Items.Add(username);//Label1.Text= username;
    }

   //now change the UserForm Form3 constructor to take 1 argument
    public Form3(string username)
    {
        InitializeComponent();
        myListBox.Items.Add(username);//Label1.Text= username;
    }