【问题标题】:Getting a value from Dataset and pass it to Globals C#从 Dataset 获取值并将其传递给 Globals C#
【发布时间】:2018-03-22 06:00:11
【问题描述】:

我想从数据库的数据集中获取一个值。我创建了一个登录表单,当我登录时,它会在 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);
            }
        } 

【问题讨论】:

    标签: c# dataset globals


    【解决方案1】:

    这里有一些问题,例如,当它实际上只有 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);
        }
    } 
    

    【讨论】:

    • 你救了我^^我没想到
    • 我更新了你的代码以避免错误,正确的代码HabibisGrll.Globals.cashier = Convert.ToInt32( ds.Tables[0].Rows[0].ItemArray[0]);非常感谢^^
    • Convert.ToInt32 在内部调用 Int32.Parse,只要 value 不为 null,所以它应该(几乎)相同。现在我再次看到它,我在那里错过了一个 ToString(),因为 itemarray 是一个对象数组,而不是字符串,所以你所做的是完美的。此外,如果要添加验证,可以使用 TryParse 来验证值(即,确保数据集对象可以转换为整数)。您的原始代码不起作用,因为您正在保存该行的索引,该索引将始终为 0,因为它只有 1 条记录。我修复了这个问题并在你的静态类中保存了正确的值。
    猜你喜欢
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2023-03-22
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2014-01-03
    相关资源
    最近更新 更多