使用C#中的两个查询填充DataTable

时间:2016-10-29 05:58:43

标签: c#

我在SQL Server数据库中有多个表,我试图在两个表上用DataTable填充两个查询。

这是我的代码:

String qry = "SELECT Count(*) FROM boss WHERE username ='" + textBox1.Text + "'and password ='" + textBox2.Text + "'; SELECT Count(*) FROM employee WHERE first_name ='" + textBox1.Text + "'and password ='" + textBox2.Text + "' "; 

SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=my_pro;Integrated Security=True");

SqlDataAdapter sda = new SqlDataAdapter(qry, con);
DataTable dt = new DataTable();
sda.Fill(dt);

但是这显示了来自boss表的数据。我该怎么办?

3 个答案:

答案 0 :(得分:0)

您需要更改查询

SELECT  (
        SELECT COUNT(*)
        FROM   boss 
        ) AS count1,
        (
        SELECT COUNT(*)
        FROM   employee 
        ) AS count2
 WHERE username ='" + textBox1.Text + "'and password ='" + textBox2.Text + "';

答案 1 :(得分:0)

试试这个:

  String qry = "SELECT Count(*) FROM boss WHERE username ='" + textBox1.Text +    
               "'and password ='" + textBox2.Text + "' UNION ALL  SELECT Count(*) FROM   
                 employee WHERE first_name ='" + textBox1.Text + "'and password ='" +  textBox2.Text + "' "; 

答案 2 :(得分:0)

您可以使用子查询

    SELECT  BossCount=(
            SELECT COUNT(*)
            FROM   boss 
            WHERE username ='" + textBox1.Text + "'and password ='" + textBox2.Text + "'
            ) ,
            EmployeeCount=(
            SELECT COUNT(*)
            FROM   employee 
            boss  WHERE first_name ='" + textBox1.Text + "'and password ='" + textBox2.Text + "'
            )