2015-02-23 78 views
0

我有這樣的代碼在我btnLoginForm1MS Access database獲得的數據,我想它使用標籤在Form2顯示,但我不知道如何將它傳遞到Form2檢索數據並顯示它以另一種形式標記

private void btnLogin_Click(object sender, EventArgs e) 
    { 
     connection.Open(); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connection; 
     if ((this.txtUser.Text == "admin") && (this.txtPass.Text == "admin")) 
     { 
      Form1 main = new Form1(); 
      main.txtHere.Text = txtUser.Text; 
      main.Show(); 
      this.Hide(); 
     } 
     else { 
      command.CommandText = "select * from StudentsTBL where LastName='" + txtUser.Text + "' and FirstName='" + txtPass.Text + "'"; 

      OleDbDataReader reader = command.ExecuteReader(); 
      int count = 0; 
      while (reader.Read()) { 
       count = count + 1; 
       //count++; 
      } 
      if (count == 1) { 
       MessageBox.Show("Login Successful!"); 
       List<String> stdNo = new List<String>(); 
       List<String> middleName = new List<String>(); 
       List<String> section = new List<String>(); 
       List<String> year = new List<String>(); 
       List<String> sem = new List<String>(); 
       List<String> address = new List<String>(); 
       List<String> dob = new List<String>(); 
       List<String> gender = new List<String>(); 
       List<String> age = new List<String>(); 
       List<String> contact = new List<String>(); 
       List<String> desc = new List<String>(); 

       command = new OleDbCommand("select * from StudentsTBL", connection); 
       reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
        stdNo.Add(reader["StudentNo"].ToString()); 
        middleName.Add(reader["Middle"].ToString()); 
        section.Add(reader["stdSection"].ToString()); 
        year.Add(reader["stdYear"].ToString()); 
        sem.Add(reader["stdSem"].ToString()); 
        address.Add(reader["stdAddress"].ToString()); 
        dob.Add(reader["stdDob"].ToString()); 
        gender.Add(reader["stdGender"].ToString()); 
        age.Add(reader["stdAge"].ToString()); 
        contact.Add(reader["ContactNo"].ToString()); 
        desc.Add(reader["stdDesc"].ToString()); 
       } 
       StudentProfile stdProfile = new StudentProfile(txtUser.Text, txtPass.Text); 
       stdProfile.Show(); 
       this.Hide(); 
      } 
      else if (count > 1) 
      { 
       MessageBox.Show("Duplicate username and password!!"); 
      } 
      else { 
       MessageBox.Show("Login Failed!"); 
      } 
     } 
     connection.Close(); 
    } 

來源:Getting data from MS Access database and display it in a listbox

+0

嗯,什麼'Form2'? 'Label'字段的名稱是什麼? – MickyD 2015-02-24 00:00:40

+0

我想顯示數據到'form2' – user3833309 2015-02-24 00:04:01

+0

[Forms are just classes](http://www.contrivedexample.com/ce/post/2015/02/15/forms-are-just-classes.aspx) – Crowcoder 2015-02-24 00:09:17

回答

0

爲什麼不使用結構? 它會讓你很容易喲可以處理單個 變量中的大量信息。

爲什麼你不做一個全局變量? 它可以從你的任何部分申請。

我有這樣的GitHub項目,你可以下載作爲實例參考

structure UserInfo{ 
    public string stdNo; 
    public string middleName; 
    public string section; 
    public string year; 
    public string sem; 
    public string address; 
    public string dob; 
    public string gender; 
    public string age; 
    public string contact; 
    public string desc; 
} 

//===>Define a global variable to handle 
public static UserInfo LoggedUsrInfo = new UserInfo(); 

private void btnLogin_Click(object sender, EventArgs e) 
    { 
     connection.Open(); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connection; 
     if ((this.txtUser.Text == "admin") && (this.txtPass.Text == "admin")) 
     { 
      Form1 main = new Form1(); 
      main.txtHere.Text = txtUser.Text; 
      main.Show(); 
      this.Hide(); 
     } 
     else { 
      command.CommandText = "select * from StudentsTBL where LastName='" + txtUser.Text + "' and FirstName='" + txtPass.Text + "'"; 
      DataTable Tbl = new DataTable(); 
      OleDbDataReader reader = command.ExecuteReader(); 
      Tbl.Load(reader,LoadOption.OverwriteChanges);//===>Retrieve data and load to datatble 
      reader.Close();//Close the reader 
      if (Tbl.Rows.Count > 0) {//Count if the data table retrieve some info 
       DataTable TblInfo = new DataTable(); 
       MessageBox.Show("Login Successful!"); 


       command = new OleDbCommand("select * from StudentsTBL", connection); 
       reader = command.ExecuteReader(); 
       TblInfo.Load(reader,LoadOption.OverwriteChanges); 
       reader.Close();//Close the reader 
        LoggedUsrInfo.stdNo = TblInfo.Rows[0]["StudentNo"].ToString(); 
        LoggedUsrInfo.middleName = TblInfo.Rows[0]["Middle"].ToString(); 
        LoggedUsrInfo.section = TblInfo.Rows[0]["stdSection"].ToString(); 
        LoggedUsrInfo.year = TblInfo.Rows[0]["stdYear"].ToString(); 
        LoggedUsrInfo.sem =TblInfo.Rows[0]["stdSem"].ToString(); 
        LoggedUsrInfo.address = TblInfo.Rows[0]["stdAddress"].ToString(); 
        LoggedUsrInfo.dob = TblInfo.Rows[0]["stdDob"].ToString(); 
        LoggedUsrInfo.gender = TblInfo.Rows[0]["stdGender"].ToString(); 
        LoggedUsrInfo.age = TblInfo.Rows[0]["stdAge"].ToString(); 
        LoggedUsrInfo.contact = TblInfo.Rows[0]["ContactNo"].ToString(); 
        LoggedUsrInfo.desc = TblInfo.Rows[0]["stdDesc"].ToString(); 

       StudentProfile stdProfile = new StudentProfile(txtUser.Text, txtPass.Text); 
       stdProfile.Show(); 
       this.Hide(); 
      } 
      else if (count > 1) 
      { 
       MessageBox.Show("Duplicate username and password!!"); 
      } 
      else { 
       MessageBox.Show("Login Failed!"); 
      } 
     } 
     connection.Close(); 
    } 


/* 
you can access to UserInfo calling in this way from your Form2 
Load Event 

Label1.Text = Form1.LoggedUsrInfo.middleName; 

*/ 
相關問題