C# 获取WinC表单中的用户ID

C# 获取WinC表单中的用户ID,c#,winforms,login,C#,Winforms,Login,我使用WinC 我试图从userinfo表中获取userid,然后将该userid插入到另一个表中。首先,我尝试 public class Login { string email; string password; public int userid; public Login(string UserEmail, string UserPassword) { email = UserEmail; password = UserPassword; } public strin

我使用WinC

我试图从userinfo表中获取userid,然后将该userid插入到另一个表中。首先,我尝试

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!";
    }
}
更新代码

好的,我试试这个

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!";
    }
}
如果需要,请单击“其他”按钮

 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,如果它是一个新表单,您可以使用该类的构造函数来传递所需的参数。 类似的东西应该有用:我也使用了一个标签来显示通过了什么

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!";
        }
    }
这是你的按钮,如果。。。第部分:

只需更改Categorys构造函数以获取整数,将其分配给Categorys类中的全局变量,然后就可以在onload方法中使用它,如
textbox1.Text=Convert.tostringcategoris\u userid的实例\u

关于我发现的问题。现在我在登录功能中添加了这个。。whiledr.Read{int userid=Convert.ToInt32dr[userid];}但我如何在按钮单击中使用此userid?请检查更新的代码您可以将userid分配给登录类中的全局变量,并在调用MessageBox.Showobjlogin.login后在按钮单击函数中访问它;当我这么做的时候。。Login objlogin=new LoginUserEmail、UserPassword、loginid;textBox3.Text=Convert.ToStringloginid;这显示了未分配的错误,无论其他任何情况,请仔细阅读,以及如何防止它和。