我在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
表的数据。我该怎么办?
答案 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 + "'
)