Results 1 to 6 of 6

Thread: [2005] Fill a dataset with 2 unrelated Tables.

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    42

    [2005] Fill a dataset with 2 unrelated Tables.

    I guess This is a two part Question: first part being How to load 2 tables from the same database where both tables can not be joined by a Common field?

    Would I have to Do 2 Different select Commands ? as in the below example.
    My second part. Im not sure how to syntax the Dataset to store the second table with fill. Here is the code I am trying to use:

    Code:
     Private Sub ManagersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Db As String
            
    
            Db = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & "\EmployeeVzwSlots.mdb"
            Con.ConnectionString = Db
            OleDbmcmnds(0) = New OleDbCommand("SELECT AwardSubject, SubjectSpinsAwarded FROM AwardedSubjects")
            OleDbmcmnds(1) = New OleDbCommand("SELECT Id, FirstName, LastName, SpinsAwarded, SpinsUsed, MoneyWon FROM EmployeeInfo")
    
            For index As Int16 = 0 To 1
                DA.SelectCommand = OleDbmcmnds(index)
                DA.SelectCommand.Connection = Con
                DA.Fill(DS.Tables(index))
            Next index

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    42

    Re: [2005] Fill a dataset with 2 unrelated Tables.

    Sorry, Just noticed I posted in the wrong section of Forum.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Fill a dataset with 2 unrelated Tables.

    vb.net Code:
    1. Dim adp1 As New OleDbCommand("SELECT * FROM Table1", con)
    2. Dim adp2 As New OleDbCommand("SELECT * FROM Table2", con)
    3.  
    4. adp1.Fill(myDataSet, "Table1")
    5. adp2.Fill(myDataSet, "Table2")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Fill a dataset with 2 unrelated Tables.

    Hello,

    Just looking at your code, but could you not use just 1 adatper

    i.e.
    vb Code:
    1. Dim da As New System.Data.SqlClient.SqlDataAdapter()
    2.             Dim cmd As New System.Data.SqlClient.SqlCommand()
    3.             Dim ds As DataSet
    4.  
    5.             cmd.CommandText = "Select * From Table1"
    6.             cmd.CommandType = CommandType.Text
    7.             da.SelectCommand = cmd
    8.             da.Fill(ds, "Table1")
    9.  
    10.             cmd.CommandText = "Select * From Table2"
    11.             cmd.CommandType = CommandType.Text
    12.             da.SelectCommand = cmd
    13.             da.Fill(ds, "Table2")
    steve

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Fill a dataset with 2 unrelated Tables.

    Quote Originally Posted by steve_rm
    Hello,

    Just looking at your code, but could you not use just 1 adatper

    i.e.
    vb Code:
    1. Dim da As New System.Data.SqlClient.SqlDataAdapter()
    2.             Dim cmd As New System.Data.SqlClient.SqlCommand()
    3.             Dim ds As DataSet
    4.  
    5.             cmd.CommandText = "Select * From Table1"
    6.             cmd.CommandType = CommandType.Text
    7.             da.SelectCommand = cmd
    8.             da.Fill(ds, "Table1")
    9.  
    10.             cmd.CommandText = "Select * From Table2"
    11.             cmd.CommandType = CommandType.Text
    12.             da.SelectCommand = cmd
    13.             da.Fill(ds, "Table2")
    You could but you wouldn't. If you are using DataAdapters then that would imply that you would need to Update the data too. If you didn't need to update the data then you should use a DataReader to load the data, in which case you could use the same Command with different CommandText each time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    42

    Resolved Re: [2005] Fill a dataset with 2 unrelated Tables.

    Didnt figure that. I am going to need to update both tables.
    Tx

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