Question datatable into dataset

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
i have a few datasets that i have fetched from an access file. And all the datasets has only 1 table in them. if i try to put all the tables into a new dataset, it say the table already belong to another dataset. How do i combine all the tables into a new dataset?
 
because i dont know how to change the fetchdata so it get the table that doesnt belong to a dataset.

VB.NET:
 Public Shared Function fetchData(ByVal fileName As String, ByVal table As String) As DataSet
        Dim oConnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fileName & ";User ID=Admin;Password="        'Define connection string
        Dim oQuery As String = "SELECT * FROM " & table        'Query String

        Dim oConn As OleDbConnection = New OleDbConnection(oConnect)        'Define the connectors
        Dim oComm As OleDbCommand = New OleDbCommand(oQuery, oConn)
        Dim oData As OleDbDataAdapter = New OleDbDataAdapter(oQuery, oConn)

        Dim resultSet As New DataSet

        Try
            oConn.Open()            'Open connection
            oData.Fill(resultSet, table)            'Fill dataset
            oConn.Close()            'Close connection

        Catch ex As OleDb.OleDbException
        Catch ex As Exception            'Show error message and exit
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)

        Finally            'Dispose the connector objects
            If Not (oConn Is Nothing) Then oConn.Dispose()
            oConn = Nothing
            If Not (oComm Is Nothing) Then oComm.Dispose()
            oComm = Nothing
            If Not (oData Is Nothing) Then oData.Dispose()
            oData = Nothing

        End Try

        Return resultSet       'Return results
    End Function
 
So if you use same DataSet object and specify different table names for each Fill you are good, that I explained in previous post.
 
right now i am getting the multiple dataset from fetchdata and then using merge to make a single dataset
 
DataSet is like an array(collection) of DataTable

Do not confuse it with a "recordset" from old ADO, just because they both end in the word "set"
Recordset = a set of records i.e. a table.. So datable(a collection of datarow) is analogous to recordset.. Actually, recordset is like a reader, because you have to move up and down it rather than access it like an array

Anyways, 1) you'll do yourself a lot of favours if you read the DW2 link in my signature, section Creating a Simple Data App; right now youre writing a lot of tedious code that VB can write for you

2) you declare a dataset, and then fill data into it, not realising that the adapter will create a new table inside that dataset for you, then fill the table (tables hold data, not sets!) and youre then getting problems trying to put that datatable into a different dataset..
..why not just create a datatable instead of a set, in the first instance, and then fill the table.. you then have a full but detached datatable that you can put in any set you like
OR
why not reuse the same set object youre filling the first time?

all these options are less cumputationally expensive than merge
 
Back
Top