Link to home
Start Free TrialLog in
Avatar of WATYF
WATYF

asked on

Copy a DataSet/DataTable to an Access Database.

Hi there... I'm having a very hard time trying to find a direct, fully-programatic way to select data from a table in SQL Server and copy it into a table in Access. There are a million "work-arounds" out there, but none of them are very efficient options. I have exhausted the options available using ADO in VB, and since this process will eventually be written in VB.NET, I figured I'd try this angle.


So here's my scenario... I open a connection to SQL Server (using an OLEDB con in .NET) I select the contents of a table (or perform any kind of SELECT query) and populate a DataTable/DataSet with the results... I then open an OLEDB con to an Access DB. I now have a DataTable (or DataSet) full of data that I want to drop directly into a new table in the Access DB.


So how do I do this?



P.S. The most common "solution" I've found up to this point involves looping through every single record and writing them to a table/text file/whatever... that's not an efficient option in my case, since I will be working with so many records.


WATYF
Avatar of iboutchkine
iboutchkine

You can use SQL to insert from one table into another in external database

For SQL Server
=============
insert into db2.dbo.table2 select * from db1.dbo.table1

For MS Access
=============
INSERT INTO Table1 IN 'ExternalDatabasePath1' SELECT *
FROM Table1 IN 'ExternalDatabasePath2';
where externaldatabasepath1 & 2 are something like c:\databases\db1.mdb & c:\databases\db2.mdb
(INSERT INTO Table1 SELECT * FROM Table2  IN 'c:/aaa/bbb/Database.mdb')

Database, which engine is understood by VB
=====================================
You can also add a type to the end of the IN clause such as "dBASE IV" e.g.,

INSERT INTO Table1 IN 'ExtDB' 'dBASE IV' SELECT * ....
Avatar of WATYF

ASKER

OK. I use the Access version of that SQL statement quite frequently... if the original data was in Access, I wouldn't even be having this problem...


...but how do you specify a remote path in the SQL version?


insert into db2.dbo.table2 select * from db1.dbo.table1


How do I specify a path and db filename of an Access database in the above example?



WATYF
insert into db2.dbo.table2 SELECT * FROM Table2  IN 'c:/aaa/bbb/Database.mdb'
Avatar of WATYF

ASKER

That would be selecting data from Access into SQL. I need it the other way around. From SQL into Access DB.

...something like:


INSERT INTO MyTable IN 'C:\MyFiles\Test.mdb' SELECT * FROM dbo.SQLTable


The problem is... you can't use the "IN" operator in SQL Server... or at least... it does something entirely different in SQL than it does in Access.


WATYF
INSERT INTO MyTable IN 'C:\MyFiles\Test.mdb' SELECT * FROM dbo.SQLTable

does this work?


Microsoft claims that it will work

Database, which engine is understood by VB
=====================================
You can also add a type to the end of the IN clause such as "dBASE IV" e.g.,

INSERT INTO Table1 IN 'ExtDB' 'dBASE IV' SELECT * ....



Another option will be to read table from SQL Server to dataset and then insert from dataset to Access. If you want to create a new table in Access use ADOX.
Avatar of WATYF

ASKER

No... that statement will not work... I get an error: Invalid Syntax near 'IN'.

In SQL Server, the "IN" operator is used to find values in a column... not to specify what Database a Table resides in. That statement will work in Access, but not in SQL Server.


And yes... the option I originally asked about was inserting from a dataset into Access... so how do I do that?


WATYF

   Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        Dim sql As String = "SELECT * FROM Publishers"
        Dim cn As New OleDbConnection(BiblioConnString)
        Dim da As New OleDbDataAdapter(sql, cn)
        Dim ds as New DataSet()

        cn.Open()
        da.Fill(ds, "Publishers")


        Dim cmdBuilder As New OleDbCommandBuilder(da)


        da.InsertCommand = cmdBuilder.GetInsertCommand


        With ds.Tables("Publishers")
            ' Add a new record
            Dim dr As DataRow = .NewRow
            dr("pub_id") = "9988"
            dr("Name") = "Iouri"
            dr("city") = "New York"
            dr("country") = "USA"
            .Rows.Add(dr)
        End With

        ' Send changes them to the database.
        da.Update(ds, "Publishers")
        cn.Close()
    End Sub

'else there was no success give a message
Avatar of WATYF

ASKER

I'm sorry, but that's not what I'm looking for either. I don't need to add a row to a datatable. I need to copy an entire table from SQL Server to Access.


WATYF
You are talking about bulk insert... Take a look here.. http://support.microsoft.com/default.aspx?scid=kb;EN-US;316244
-Baan
Avatar of WATYF

ASKER

Actually, bulk insert would be if I wanted to import into SQL. I want to export out of SQL.

And I can't use the DTS wrapper because SQL Server isn't installed on the client machines that will be running this code.


WATYF
Then the only way out is to loop through the records. Lets see what other experts say.
-Baan
ASKER CERTIFIED SOLUTION
Avatar of WATYF
WATYF

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial