Using System; Using System. Collections. Generic; Using System. ComponentModel; Using System. Data; Using System. Drawing; Using System. Text; Using System. Windows. Forms; Using System. Data. SqlClient; Using System. Linq; Namespace LinkTwoData { Public partial class Form1: Form { Public Form1 () { InitializeComponent (); } String strcon1 = @ "Data Source = FENG-PCSQLEXPRESS; Initial Catalog = test1; User ID = sa; PassWord = sa2008 "; String strcon2 = @ "Data Source = FENG-PCSQLEXPRESS; Initial Catalog = test2; User ID = sa; PassWord = sa2008 "; Private void Form1_Load (object sender, EventArgs e) { SqlDataAdapter sda1 = new SqlDataAdapter ("select * from stu1", strcon1 ); SqlDataAdapter sda2 = new SqlDataAdapter ("select * from stu1", strcon2 ); DataSet ds = new DataSet (); Sda1.Fill (ds, "stu1 "); Sda2.Fill (ds, "stu2 "); Var query = from stu in ds. Tables ["stu1"]. AsEnumerable () From SC in ds. Tables ["stu2"]. AsEnumerable () Where stu. Field <int> ("SC") = SC. Field <int> ("SC ") Select new { Sno = stu. Field <int> ("sno", DataRowVersion. Original ), Sname = stu. Field <string> ("sname", DataRowVersion. Original ), Sage = stu. Field <int> ("sage", DataRowVersion. Original ), Scname = SC. Field <string> ("scname", DataRowVersion. Original) }; DataTable dt = new DataTable (); Dt. Columns. Add ("sno", typeof (int )); Dt. Columns. Add ("sname", typeof (string )); Dt. Columns. Add ("sage", typeof (string )); Dt. Columns. Add ("scname", typeof (string )); Foreach (var item in query) { DataRow newRow = dt. NewRow (); NewRow ["sno"] = item. sno; NewRow ["sname"] = item. sname; NewRow ["sage"] = item. sage; NewRow ["scname"] = item. scname; Dt. Rows. Add (newRow ); } DataGridView1.DataSource = dt. DefaultView; } } } |