从 C# 表单在两个访问表中搜索

search inside two access tables from C# form

这是我的代码,我如何在 Table1 和 Table2 中搜索具有相同 textbox1.text 值的内容,换句话说:

// [表1]

| name     | age            |
| -------- | -------------- |
| aaa      | 12             |
| bbb      | 13             |
| ccc      | 14             |

//     [Table2]
| name     | gender         |
| -------- | -------------- |
| aaa      | male           |
| bbb      | female         |
| ccc      | male           |

当 [name] 等于 textbox1.text 时,如何获取年龄和性别值并将其显示在 txt.Text 中?

thanks

 con.Open();
            // search inside table1
            OleDbCommand da = new OleDbCommand("SELECT * FROM [Table1] WHERE Table1.name='@name",con);
 
           //search inside table2
            OleDbCommand da2 = new OleDbCommand("SELECT * FROM [Table2] WHERE Table2.name='@name", con);
            
            // give @name value to table1 and table2 as parameters
            da.Parameters.AddWithValue("@name",textBox1.Text);
            da2.Parameters.AddWithValue("@name", textBox1.Text);

            // its time for executing 
            OleDbDataReader dr = da.ExecuteReader();
            OleDbDataReader dr2 = da2.ExecuteReader();

            // now display it in textBox as a string
            if (dr.Read())
            {
                dr2.Read();
                for (int x = 0; x < 4;x++ ) {
                    if(dr[x].ToString() !=""){

                        txt.Text += dr[x].ToString() + System.Environment.NewLine;
                    }
                }
            }
            else { MessageBox.Show("error"); }
            con.Close();

将数据连接在一起并限制为仅写入文本框并显示在 datagridview 控件中的内容:

using(var da = new OleDbDataAdapter("SELECT FROM Table1 t1 INNER JOIN Table2 t2 ON t1.Name = t2.Name WHERE t1.name=@name", con)){
    da.SelectCommand.Parameters.AddWithValue("@name",textBox1.Text);

    var dt = new DataTable();
    da.Fill(dt);

    someDataGridView.DataSource = dt;
}

这就是您需要做的全部;数据适配器将打开连接等;只需写一个sql,添加参数,填充数据table,将table指定为网格的DataSource(您需要向表单添加一个datagridview并命名它并调整代号)