Asp.net simultaneously queries two database tables for joint Query

Source: Internet
Author: User

This article introduces a friend who needs to know about asp.net's simultaneous query of two database tables for joint query.

Recently, I was asked a question about how to perform joint queries on the tables in the two databases.

Then I implemented it in the most stupid way. Hope you can see a good solution, just use a connection string. It is best to give a detailed tutorial.

 

First, there are two such databases, each with two tables.

The data in the table is also very simple, that is, the student table and the professional table are associated with the professional number.

Next, bind data to the Winfrom DataGridView to display the student ID, name, age, and major. The effect is as follows:

Because I have used linq to dataset for data operations, the. net version of the project is later than 3.5.

The following code binds data:

The Code is as follows: Copy code

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;

}

}
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.