Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to join 2 tables in a data set

how to join 2 tables in a data set

Scheduled Pinned Locked Moved C#
csharpdatabaselinqtutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nine_
    wrote on last edited by
    #1

    Hi all I have 2 tables in the same data set like this:

    string sqlCus="select * from Customers";
    string sqlOrder="select * from Orders";

    SqlDataAdapter daCus=new SqlDataAdapter(sqlCus,conn);
    SqlDataAdapter daOrder=new SqlDataAdapter(sqlOrder,conn);

    DataSet ds=new DataSet();
    daCus.Fill(ds,"Customers");
    daOrder.Fill(ds,"Orders");

    now I want to get the OrderID column in Orders table and ContactName (CustomerName) in the Customers table according to the CustomerID column in the Orders table then show both column in a DataGridView control, how to do that? note: not use stored procedure and Linq

    T 1 Reply Last reply
    0
    • N Nine_

      Hi all I have 2 tables in the same data set like this:

      string sqlCus="select * from Customers";
      string sqlOrder="select * from Orders";

      SqlDataAdapter daCus=new SqlDataAdapter(sqlCus,conn);
      SqlDataAdapter daOrder=new SqlDataAdapter(sqlOrder,conn);

      DataSet ds=new DataSet();
      daCus.Fill(ds,"Customers");
      daOrder.Fill(ds,"Orders");

      now I want to get the OrderID column in Orders table and ContactName (CustomerName) in the Customers table according to the CustomerID column in the Orders table then show both column in a DataGridView control, how to do that? note: not use stored procedure and Linq

      T Offline
      T Offline
      tech603
      wrote on last edited by
      #2

      You could do one query and join the tables on the query string sql = "select ContactName, Orders.OrderID from Customers Left Outer Join Order ON CustomerID = Order.CustomerID"; The above should work, you may just have to change the column names to what you want. You could also add an Order by statement in the end of that if you wanted. This link may help you in the future with joining tables in sql queries. http://en.wikipedia.org/wiki/Join_(SQL)[^] hope that helps you. Matthew Vass QA Analyst mvass@hostmysite.com http://www.hostmysite.com?utm_source=bb[^]

      N 1 Reply Last reply
      0
      • T tech603

        You could do one query and join the tables on the query string sql = "select ContactName, Orders.OrderID from Customers Left Outer Join Order ON CustomerID = Order.CustomerID"; The above should work, you may just have to change the column names to what you want. You could also add an Order by statement in the end of that if you wanted. This link may help you in the future with joining tables in sql queries. http://en.wikipedia.org/wiki/Join_(SQL)[^] hope that helps you. Matthew Vass QA Analyst mvass@hostmysite.com http://www.hostmysite.com?utm_source=bb[^]

        N Offline
        N Offline
        Nine_
        wrote on last edited by
        #3

        Thank you for your answer, but what I wanted to know is how to join 2 tables in the same data set using data relation. I do know how to join 2 tables using sql's select command, but this one is not. Could you please point me out for this? Thanks

        T 1 Reply Last reply
        0
        • N Nine_

          Thank you for your answer, but what I wanted to know is how to join 2 tables in the same data set using data relation. I do know how to join 2 tables using sql's select command, but this one is not. Could you please point me out for this? Thanks

          T Offline
          T Offline
          tech603
          wrote on last edited by
          #4

          I'm sorry the first answer did not help you, I'm guessing you have a reason to populate two separate datasets and then parse through them to get your info. You may want to look at this article, it explains how to use select/sort things using your dataset and a datatable. http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx[^] Also you will most likely have to do a dataset copy then dataset.add to add the columns to the copied dataset in order to get the results you want. Not sure if this is exactly what your looking for but hope this points you in the right direction.

          Matthew Vass QA Analyst mvass@hostmysite.com HostMySite.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups