@Martin Wang , Welcome to Microsoft Q&A, you could convert two tables in database to two datatables separately.
I make a code example and you could have a look.
private void button1_Click(object sender, EventArgs e)
{
DataTable table1 = GetDataTable1();
DataTable table2 = GetDataTable2();
var results = from t1 in table1.AsEnumerable()
join t2 in table2.AsEnumerable() on t1.Field<string>("StuName") equals t2.Field<string>("StuName")
select new
{
StuName = t1.Field<string>("StuName"),
SubjectName = t2.Field<string>("SubjectName"),
Score = t2.Field<int>("Score"),
StuAge = t1.Field<int>("StuAge")
};
dataGridView1.DataSource = results.ToList() ;
}
public DataTable GetDataTable1()
{
string connstr = "connstr";
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
string sql = "select * from StuInfo ";
DataTable table1 = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(table1);
connection.Close();
return table1;
}
public DataTable GetDataTable2()
{
string connstr = "connstr";
MySqlConnection connection = new MySqlConnection(connstr);
connection.Open();
string sql = "select * from ScoreInfo ";
DataTable table2 = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);
adapter.Fill(table2);
connection.Close();
return table2;
}
Tested result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.