Reputation: 11
I am trying to query from multiple tables in one query.
Although my code is working for one table, I don't know how to search in the second table too.
using (DataTable dt = new DataTable("Uniclass2015_EF_v1_12; Uniclass2015_En_v1_26"))
{
using (SqlCommand cmd = new SqlCommand (" select *from Uniclass2015_En_v1_26 where title like @Title; select *from Uniclass2015_EF_v1_12 where title like @Title", conn))
{
cmd.Parameters.AddWithValue("code", txtSearch.Text);
cmd.Parameters.AddWithValue("Title", string.Format("%{0}%", txtSearch.Text));
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
conn.Close();
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) // enter
BtnSearch.PerformClick();
}
I tried to add a second table to my original code, but however when I type in the search box my query I receive a blank answer.
Upvotes: 1
Views: 73
Reputation: 85
looks like you're wanting to use the UNION operator in SQL:
https://www.w3schools.com/sql/sql_union.asp
SELECT * FROM Uniclass2015_En_v1_26 WHERE title LIKE @Title UNION
SELECT * FROM Uniclass2015_EF_v1_12 WHERE title LIKE @Title
This of course assumes the columns of the two tables are the same. If you only need specific columns from each, just select those columns.
Also, I don't see where you're using that code
parameter you're adding
Upvotes: 2