[英]How to display the logged user in listbox c#
所以我有一個登錄系統,當用戶登錄時,會打開一個新表格,我希望該表格顯示用戶名。 當用戶注冊時,他必須輸入他的名字和姓氏,而這2件事我想在打開的新表格中顯示。 那么,有什么簡單的方法可以將登錄的用戶名放入列表框?
所以這是注冊碼:
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);
}
您可以將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)和用戶表單(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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.