asp.net中同時查詢兩個資料庫表進行聯集查詢

來源:互聯網
上載者:User

最近被問道了一個問題,就是如何對兩個資料庫裡面的表進行聯集查詢。

後來我就用了最笨的方法實現了。希望看到的朋友能給個好的解決方案,只用一個連接字串。最好是給個詳細的教程。

 

首先有這樣的兩個資料庫,每個資料庫中有兩個表。

表裡面的資料也很簡單,就是學生表和專業表,用專業號關聯。

下面就在Winfrom的DataGridView上綁定資料,顯示學生的編號、姓名、年齡和專業。效果如下:

由於對資料的操作我用到了linq to dataset 所以項目的.net 版本為3.5以上。

下面就是綁定資料的代碼:

 代碼如下 複製代碼

 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;

         }

      }
  }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.