从数据集中获取值并将其传递给 Globals C#

Getting a value from Dataset and pass it to Globals C#

我想从数据库的数据集中获取一个值。我创建了一个登录表单,当我登录时,它会在 MessageBox 中显示用户的 ID 号和用户名。

当我尝试将 ID 值传递给全局变量时,我得到 0。我正在使用 for 循环和 while 循环。每次我将该值传递给其他形式时,我仍然得到 0。我不知道我的代码中有什么问题。

你能给我指出正确的方向吗?

int i ;
HabibisGrll.Globals.cashier = i;

private void but_log_in_Click(object sender, EventArgs e)
        {

            if (tbx_username.Text == "" || Tbx_Password.Text == "")
            {
                MessageBox.Show("Please provide UserName and Password");
                return;
            }
            try
            {
                //Create SqlConnection
                using (SqlConnection con = new SqlConnection(connectionString))
                using (SqlCommand cmd = new SqlCommand("Select * from Tbl_Cashier where FName=@username and Password=@password", con))
                using (SqlDataAdapter adapt = new SqlDataAdapter(cmd))

                {
                    con.Open();
                    cmd.Parameters.AddWithValue("@username", tbx_username.Text);
                    cmd.Parameters.AddWithValue("@password", Tbx_Password.Text);

                    HabibisGrll.Globals.sss = tbx_username.Text;

                    DataSet ds = new DataSet();
                    adapt.Fill(ds);
                    con.Close();
                    int count = ds.Tables[0].Rows.Count;


                    //while(i <= ds.Tables[0].Rows.Count - 1)
                    //{
                    //    MessageBox.Show("ID Number : " + ds.Tables[0].Rows[i].ItemArray[0] + Environment.NewLine + "USER : " + ds.Tables[0].Rows[i].ItemArray[1], "User INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    //    i++;
                    //}

                    for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                    {
                        MessageBox.Show("ID Number : " + ds.Tables[0].Rows[i].ItemArray[0] + Environment.NewLine + "USER : " + ds.Tables[0].Rows[i].ItemArray[1], "User INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    //If count is equal to 1, than show frmMain form
                    if (count == 1)
                    {
                        this.Hide();
                        HabibisGrll fm = new HabibisGrll();
                        fm.Show();
                    }
                    else
                    {
                        MessageBox.Show("Login Failed!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        } 

这里有一些错误,例如当它实际上只有 1 条记录时使用 for 循环有点奇怪,但无论如何,这是我认为你应该做的,改变尽可能少的代码:

private void but_log_in_Click(object sender, EventArgs e)
{

    if (tbx_username.Text == "" || Tbx_Password.Text == "")
    {
        MessageBox.Show("Please provide UserName and Password");
        return;
    }
    try
    {
        //Create SqlConnection
        using (SqlConnection con = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand("Select * from Tbl_Cashier where FName=@username and Password=@password", con))
        using (SqlDataAdapter adapt = new SqlDataAdapter(cmd))

        {
            con.Open();
            cmd.Parameters.AddWithValue("@username", tbx_username.Text);
            cmd.Parameters.AddWithValue("@password", Tbx_Password.Text);

            HabibisGrll.Globals.sss = tbx_username.Text;

            DataSet ds = new DataSet();
            adapt.Fill(ds);
            con.Close();
            int count = ds.Tables[0].Rows.Count;

            //If count is equal to 1, than show frmMain form
            if (count == 1)
            {
                HabibisGrll.Globals.cashier = Int32.Parse(ds.Tables[0].Rows[0].ItemArray[0].ToString());
                MessageBox.Show("ID Number : " + ds.Tables[0].Rows[0].ItemArray[0] + Environment.NewLine + "USER : " + ds.Tables[0].Rows[0].ItemArray[1], "User INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();
                HabibisGrll fm = new HabibisGrll();
                fm.Show();
            }
            else
            {
                MessageBox.Show("Login Failed!");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}