C#登录表单(将用户名从1表单传递到另一个访问数据库)

时间:2016-05-25 16:00:42

标签: c#

我有2个表格(1)登录(2)用户基本用户类型用户名&密码检查数据库是否发现它们被引导到(2)用户我想将表单(1)中的用户名传递给(2)所以我可以参考它并以(2)形式显示用户帐户信息。我是这个代码,但不确定它是否正确,因为它不会显示在表单2的文本框(用户名)中。

//表(1)

   public FormLogin()
    {
        InitializeComponent();
    }
        public string StrUsername{
        get { return txtboxUser.Text; }
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Hide();
        Main ss = new Main();
        ss.Show();
        txtboxUser.Text = String.Empty;
        txtboxPass.Text = String.Empty;
    }


    private void btnEnter_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\Graded unit Dev\BlackMarch\BlackMarch\bin\Debug\DataBaseBM.mdf;Integrated Security=True;Connect Timeout=30");
        SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From UserData where Username= '" + txtboxUser.Text + "' and Password = '" + txtboxPass.Text + "'", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);




        if (dt.Rows[0][0].ToString() == "1")
        {
            this.Hide();
            User ss = new User();
            ss.Show();
        }
        else
        {
            MessageBox.Show("Wrong Username Password");

        }
    }

//表单用户(2)

 private void btnHotelResort_Click(object sender, EventArgs e)
    {
        panelPicture.Visible = false;
        var formlogin1 = new FormLogin();
        txtUsernameUser.Text = formlogin1.StrUsername;

    }

2 个答案:

答案 0 :(得分:2)

User形式添加属性并将其设置为成功登录:

public string UserName {get; set;} // In User form

登录后:

this.Hide();
User ss = new User();
ss.UserName = txtboxUser.Text;
ss.Show();

然后,您可以访问UserName

中的User
txtUsernameUser.Text = UserName;

答案 1 :(得分:0)

您的FormUser(2)必须是:

public FormLogin frm;
 public FormUser (FormLogin frm)
    {
        InitializeComponent();
        this.frm=frm
    }

 private void btnHotelResort_Click(object sender, EventArgs e)
    {
        panelPicture.Visible = false;
        txtUsernameUser.Text = frm.StrUsername;

    }

在你的From1:

private void btnExit_Click(object sender, EventArgs e)
    {
        this.Hide();
        Main ss = new Main(this);
        ss.Show();
        txtboxUser.Text = String.Empty;
        txtboxPass.Text = String.Empty;
    }