2016-10-02 158 views
0

我正在建立一個桌面應用程序,當用戶在新的ID中登錄它時,它將出現在文本框中。但在我的情況下,查詢運行成功,但ID不出現在文本框..任何人都可以幫我找到它嗎?c#:如何從數據庫中獲取數據並傳遞給另一個表單?

用戶登錄的第一種形式(Form1.cs的)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 


namespace EmployeeApp 
{ 
    public partial class login : Form 
    { 
     public login() 
     { 
      InitializeComponent(); 
     } 

     public string employeeID; 
     private void exitButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
     private void loginButton_Click(object sender, EventArgs e) 
     { 
      SqlConnection connection = new SqlConnection(@"Data Source=INCEPSYS-SE\TEST;Initial Catalog=Employee;Integrated Security=True"); 
      connection.Open(); 
      String query = "select * from Employees where Name = '" + nameTextBox.Text + " ' and Password = '" + passwordTextBox.Text + "'"; 

      SqlCommand command = new SqlCommand(query, connection); 
      SqlDataReader myReader = command.ExecuteReader(); 
      while (myReader.Read()) 
      { 
       string employeeID = myReader["EmployeeID"].ToString(); 
      } 
      myReader.Close(); 
      SqlDataAdapter sda = new SqlDataAdapter(query,connection);    
      connection.Close(); 
      DataTable dt = new DataTable(); 
      sda.Fill(dt); 
       if (dt.Rows.Count == 1) 
      { 
       this.Hide(); 
       Entry ss = new Entry(employeeID); 
       ss.Show(); 
      } 
      else 
      { 
       MessageBox.Show("Please Check your Username & password"); 
      } 
     } 

    } 
} 

第二種形式(Entry.cs)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace EmployeeApp 
{ 
    public partial class Entry : Form 
    { 
     public Entry() 
     { 
      InitializeComponent(); 
     } 
     public Entry(string employeeId) 
     { 
       InitializeComponent(); 
       idTextBox.Text = employeeId; 
     } 


     private void reportButton_Click(object sender, EventArgs e) 
     { 
      Report report = new Report(); 
      report.Show(); 
     } 
    } 
} 

回答

2

刪除局部變量聲明,因爲employeeID是一個全局變量,並且已經宣佈首先,所以當你用string作爲它的前綴時,它會創建另一個局部變量,這個變量在這個範圍之外是不可訪問的

while (myReader.Read()) 
    { 
     employeeID = myReader["EmployeeID"].ToString(); 
    } 
0

你有一個局部變量。您可以像這樣糾正並優化您的代碼:

private void loginButton_Click(object sender, EventArgs e) 
    { 
     //If use set quote into your textbox 
     string name = nameTextBox.Text.Replace("'", "''"); 
     string pass = passwordTextBox.Text.Replace("'", "''"); 

     String query = string.Format("select * from Employees where Name = '{0}' and Password = '{1}'", name, pass); 
     string employeeID = ""; 


     using (SqlConnection connection = new SqlConnection(@"Data Source=INCEPSYS-SE\TEST;Initial Catalog=Employee;Integrated Security=True")) 
     { 
      connection.Open(); 

      using (SqlDataAdapter sda = new SqlDataAdapter(query, connection)) 
      { 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 

       if (dt.Rows.Count > 0) 
       { 
        employeeID = dt.Rows[0]["EmployeeID"].ToString(); 
        this.Hide(); 
        Entry ss = new Entry(employeeID); 
        ss.Show(); 
       } 
       else 
       { 
        MessageBox.Show("Please Check your Username & password"); 
       } 

       dt.Dispose(); 

      } 
     } 



    } 
相關問題