我在Access数据库中有2个表。目前我在VS 2015中有一个c#winform应用程序,其中有2个表单,每个表单上都有一个搜索按钮,用于搜索该特定表格唯一的相似之处是SerialNumber字段,但它不相关,因为每个表的序列号用于不同的设备。有关如何使用1个按钮搜索两个表的任何提示?
private void searchItembtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(serialSearch.Text))
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.Transaction = transaction;
command.Parameters.Add("@searchSerial", OleDbType.VarWChar).Value = serialSearch.Text;
string searchFB = "SELECT * FROM Inventory WHERE SerialNumber = @searchSerial";
command.CommandText = searchFB;
connection.Close();
OleDbDataAdapter db = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
db.Fill(dt);
dataGridFB.DataSource = dt;
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message);
connection.Close();
}
searchHide();
connection.Close();
}
}
答案 0 :(得分:0)
在Access中创建一个UNION查询,该查询将包含来自两个表的数据:
SELECT
SerialNumber,
InventoryField1 AS Field1,
InventoryFieldN AS FieldN
FROM Inventory
UNION ALL
SELECT
SerialNumber,
CompetitiveField1 AS Field1,
CompetitiveFieldN AS FieldN
FROM Competitive
然后使用此查询作为命令中的行源(CompetitiveInventory将是查询名称):
string searchFB = "SELECT * FROM CompetitiveInventory WHERE SerialNumber=@searchSerial";
或者,JOIN
可能是您想要的,具体取决于要求。