2017-04-15 49 views
0

我使用的WinForms C#获得用户ID的WinForms C#

我试图从用户信息表中获取用户ID,然后插入另一台为这一点,用户ID首先,我尝试这

更新的代码

OK我尝试这个

public class Login 
{ 
string email; 
string password; 
public int userid; 

public Login(string UserEmail, string UserPassword) 
{ 
    email = UserEmail; 
    password = UserPassword; 
} 

public string Logined() 
{ 
    con.Open(); 
    cmd = new SqlCommand("SELECT userid from UserInformation WHERE UserEmail='" + email + "'and UserPassword='" + password + "'", con); 
    cmd.Parameters.AddWithValue("@UserEmail", email); 
    cmd.Parameters.AddWithValue("@UserPassword", password); 
    cmd.ExecuteNonQuery(); 
    SqlDataReader dr = cmd.ExecuteReader(); 

    if (dr.HasRows) 
    { 
     Categorys obj = new Categorys(); 
     obj.ShowDialog(); 
     while (dr.Read()) 
     { 
      userid = Convert.ToInt32(dr["userid"]); 
     } 
     return msg = "You Are Successfully Login!"; 
    } 
    else 
    { 
     return msg = "Sorry Incorrect Password Or Email!"; 
    } 
} 

这为您button_click否则,如果

else if (comboBox1.Text == "Admin.") 
      { 
       this.Hide(); 
       string UserEmail = textBox2.Text; 
       string UserPassword = textBox1.Text; 
       Login objlogin = new Login(UserEmail, UserPassword); 
       textBox3.Text = Convert.ToString(objlogin.userid); 
       MessageBox.Show(objlogin.Logined()); 
       savedid = objlogin.userid; 
       Categorys obj = new Categorys(); 
       obj.ShowDialog(); 
       con.Close(); 
      } 

,并在新的形式,我试试这件形式负载呼叫ID

private void Categorys_Load(object sender, EventArgs e) 
    { 
     Login_Form l = new Login_Form(); 
     textBox2.Text = Convert.ToString(l.savedid); 
    } 

但这显示0中的文本框

+0

无论用它在onload方法的其他任何,请阅读[SQL注入](http://stackoverflow.com/search?q=sql+injection+%5B.net%5D)以及如何防止它和[如何将密码存储在数据库中](http ://stackoverflow.com/search q =商店+密码+在+数据库+%5B.net%5D)。 –

回答

1

如果它是你可以使用这个类的构造函数传递一个新的窗体你想要的参数。
类似的东西应该工作:(我使用的标签也显示什么已通过)

public partial class Form2 : Form 
{ 
    string rec; 
    public Form2(string rec) 
    { 
     InitializeComponent(); 
     this.rec = rec; 
    } 
    private void Form2_Load(object sender, EventArgs e) 
    { 
     label1.Text = rec; 
    } 
} 

,然后调用它像:

Form secondForm = new Form2("Hello there"); 
secondForm.ShowDialog(); 

为了您的更新版本 尝试此您的登录类

public class Login 
{ 
    string email; 
    string password; 
    public int userid; 

    public Login(string UserEmail, string UserPassword) 
    { 
     email = UserEmail; 
     password = UserPassword; 
    } 

    public string Logined() 
    { 
     con.Open(); 
     cmd = new SqlCommand("SELECT userid from UserInformation WHERE UserEmail='" + email + "'and UserPassword='" + password + "'", con); 
     cmd.Parameters.AddWithValue("@UserEmail", email); 
     cmd.Parameters.AddWithValue("@UserPassword", password); 
     cmd.ExecuteNonQuery(); 
     SqlDataReader dr = cmd.ExecuteReader(); 

     if (dr.HasRows) 
     { 
      Categorys obj = new Categorys(); 
      obj.ShowDialog(); 
      while (dr.Read()) 
      { 
       userid = Convert.ToInt32(dr["userid"]); 
      } 
      return msg = "You Are Successfully Login!"; 
     } 
     else 
     { 
      return msg = "Sorry Incorrect Password Or Email!"; 
     } 
    } 

而这个为你的button_click else if...部分:

else if (comboBox1.Text == "Admin.") 
     { 
      this.Hide(); 
      string UserEmail = textBox2.Text; 
      string UserPassword = textBox1.Text; 
      Login objlogin = new Login(UserEmail, 
      MessageBox.Show(objlogin.Logined()); 
      textBox3.Text = Convert.ToString(objlogin.userid); 
      Categorys obj = new Categorys(objlogin.userid); 
      obj.ShowDialog(); 
      con.Close(); 
     } 

只要改变的categorys构造采用整数,把它分配给在的categorys类的全局变量,然后就可以像 textbox1.Text = Convert.toString(categorys_instance_of_userid)

+0

好的,但我如何从标签中的选择语句获得用户ID? –

+0

关于我发现[这](http://stackoverflow.com/questions/4018114/read-data-from-sqldatareader)的问题。它可以帮助你 – Pio

+0

好吧,现在我把它添加到登录函数中。while(dr.Read()) int userid = Convert.ToInt32(dr [“userid”]); }但我如何使用此按钮点击userid? –