Results 1 to 9 of 9

Thread: How do I create a SQL join query using multiple DataTables?

Threaded View

  1. #1

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    How do I create a SQL join query using multiple DataTables?

    I'm trying to create a more simplistic view builder in Visual Studio/VB.net. The number of DataTables being used will vary, but we'll use 3 as an example. I have 1 DataTable that is Addresses with a "Code" field, and 2 Matrix DataTables, each with a corresponding "Code" field that will be used to map to the address list. For example:

    Address Input:
    First
    Last
    Address
    City
    State
    Zip
    Code

    Matrix 1 Input
    Code
    Offer1
    Date1

    Matrix 2 Input
    Code
    Offer2
    Date2

    Combined Output:
    First
    Last
    Address
    City
    State
    Zip
    Code
    Offer1
    Date1
    Offer2
    Date2

    This is easily done in SQL using an Inner Join Query, which I would like to replicate in VB.net. I am having trouble with syntax and connection strings to the multiple DataTables involved. Here is what I have so far: (Keep in mind that all 3 datatables in the example have been populated already with data)

    Code:
    Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=dBase IV;Data Source='Address'")
    
            Dim sql As String = "SELECT Address.FIRST, Address.LAST, Address.ADDRESS, Address.CITY, Address.STATE, Address.ZIP, Address.CODE, Matrix1.OFFER1, Matrix1.DATE1, Matrix2.OFFER1, Matrix2.DATE1
    							 FROM Matrix2 INNER JOIN (Matrix1 INNER JOIN Address ON Matrix1.CODE = Address.CODE) ON Matrix2.CODE = Address.CODE;"
            cn.Open()
            
            Dim da As OleDbDataAdapter = New OleDbDataAdapter
    		
            Dim CombinedDT As New DataTable
    		
            Using da As New OleDbDataAdapter(sql, cn)
    
                da.FillSchema(CombinedDT, SchemaType.Source)
                da.Fill(CombinedDT)
    
            End Using
    Note: I am trying to put the combined data into a new DataTable

    Any help would be greatly appreciated!!

    Thank you!
    Last edited by Christhemist; May 4th, 2018 at 10:50 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width