我如何更正位置 0 处没有行

问题描述

我想构建一个用户窗口表单,但每当我运行代码时,我都会收到错误

位置 0 处没有行

   private void button1_Click(object sender,EventArgs e)
        {
            sqlConnection con = new sqlConnection(@"Data Source=DESKTOP-LAI8526\sqlEXPRESS;Initial Catalog=user_dt;Integrated Security=True");
            sqlCommand cmd = new sqlCommand("select * from lgn_table where user_id='" + textBox1.Text + "'and user_psd='" + textBox2.Text + "' and user_type='" + comboBox1.Text + "' ",con);
            sqlDataAdapter sda = new sqlDataAdapter(cmd);
            DataTable dt = new DataTable();
                sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                sqlDataAdapter sda1 = new sqlDataAdapter("select user_type from lgn_table where user_id='" + textBox1.Text + "'and user_psd='" + textBox2.Text + "'",con);
                DataTable dt1 = new DataTable();
                sda1.Fill(dt1);
                if (dt1.Rows[0][0].ToString() == "admin")
                {
                    Form2 f = new Form2();
                    f.Show();
                    this.Hide();
                }
                if (dt1.Rows[0][0].ToString() == "employee")
                {
                    Form1 d = new Form1();
                    d.Show();
                    this.Hide();
                }

            }
        }
    }

解决方法

正在尝试更改为

DataTable dt = new DataTable();
sda.Fill(dt);

if(dt.Rows.Count==0)
    return;

如果没有行,这将退出函数