Simultaneous query of two database tables in asp.net

Source: Internet
Author: User

A question recently asked was how to make a federated query of tables in two databases.

Then I used the stupidest method to achieve it. Want to see a friend can give a good solution, with only one connection string. It's best to give a detailed tutorial.

First there are two databases with two tables in each database.

The data in the table is also very simple, is the Student table and professional table, with professional Number association.

The following data is tied to the DataGridView in Winfrom, showing the student's number, name, age, and major. The effect is as follows:

Because of the operation of the data I used the LINQ to DataSet so the project's. NET version is more than 3.5.

The following is the code that binds the 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.