Solved

Copy data from an access(2000) table into sql server(2005)

Posted on 2006-07-18
7
192 Views
Last Modified: 2010-04-23
I have two tables identical; one in Access and the other in SQL server. I want to get data from the access table customer and insert it into the sql server table customer. I'm approaching this using datasets and data adaptors.
I'm mainly not sure how to update the data from the dataset into the sql server table customer. Im using 100% code(No wizards)! Any help? Here is what Ive done so far...
------------------------------------------------------------------------------------
Dim conAccess As System.Data.OleDb.OleDbConnection
        conAccess = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\DatabaseEG.Open()

        Dim daCustomerAccess As System.Data.OleDb.OleDbDataAdapter
        Dim daOtherDataSet As System.Data.OleDb.OleDbDataAdapter
        Dim dsCustomerAccess As DataSet = New DataSet
        Dim dsOtherDataSet As DataSet = New DataSet
        Dim cmdBuildCustomer As System.Data.OleDb.OleDbCommandBuilder

        Dim strSQL As String = "Select * from Customer;"
        daCustomerAccess = New OleDb.OleDbDataAdapter(strSQL, conAccess)
        daCustomerAccess.Fill(dsCustomerAccess, "Customer")
        cmdBuildCustomer = New OleDb.OleDbCommandBuilder(daCustomerAccess)


        Dim r1 As DataRow

        For Each CustRow As DataRow In dsCustomerAccess.Tables("Customer").Rows

            r1 = dsOtherDataSet.Tables("Customer").NewRow
            r1("Customer") = CustRow("Customer")
            dsOtherDataSet.Tables("Customer").Rows.Add(r1)

        Next CustRow

        Dim conSQL As SqlClient.SqlConnection = New SqlClient.SqlConnection()
        conSQL.ConnectionString = "Data source=CRYANK; initial catalog=Sales; user id=sa;"
        conSQL.Open()

        daOtherDataSet.Update(dsOtherDataSet, "Customer")
-------------------------------------------------------------------------------------------
Any coding help would be greatly appreciated?
0
Comment
Question by:Eamon
  • 4
  • 2
7 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
If your dataset is large, you'll find that this approach can be exceedingly slow. You may want to consider using DTS from the SQL server, since it was intended for this task.

However...

The missing piece is the creation of the update command for your adapter. Assuming that you are updating a single table that is identical on the SQL server and the Access database:

    Dim strSQL As String = "SELECT * FROM Customer"

    ' Create Access-related ADO.NET objects
    Dim conAccess As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\DatabaseEG.Open")
    Dim cmdAccess As New System.Data.OleDb.OleDbCommand(strSQL, conAccess)
    Dim dtCustomerAccess As New DataTable
    Dim daCustomerAccess As New System.Data.OleDb.OleDbDataAdapter(strSQL, conAccess)
    daCustomerAccess.Fill(dtCustomerAccess)

    ' Create the SQL-related ADO.NET objects
    Dim conSQL As New SqlClient.SqlConnection("Data source=CRYANK; initial catalog=Sales; user id=sa;")
    Dim cmdSQL As New SqlClient.SqlCommand(strSQL & " WHERE 1=2", conSQL)
    Dim dtCustomerSQL As New DataTable
    Dim daCustomerSQL As New SqlClient.SqlDataAdapter(cmdSQL)

    ' Retrieve the schema for the SQL table
    daCustomerSQL.Fill(dtCustomerSQL)

    ' Set the update query
    Dim cb As New SqlClient.SqlCommandBuilder(daCustomerSQL)
    daCustomerSQL.UpdateCommand = cb.GetUpdateCommand

    For Each CustRow As DataRow In dtCustomerAccess.Rows
      Dim row As DataRow = dtCustomerSQL.NewRow

      row("Customer") = CustRow("Customer")
      dtCustomerSQL.Rows.Add(row)
    Next CustRow

    daCustomerSQL.Update(dtCustomerSQL)
0
 
LVL 1

Author Comment

by:Eamon
Comment Utility
Column 'customer' does not belong to table . is being thrown up from
row("customer") = CustRow("customer") when I run it??

Also what doe the " WHERE 1=2" mean???
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
You don't need any of the data from the SQL server table. The WHERE 1=2 will return no rows, but will return the table schema (i.e. column names and types).

The error you are getting is because either the SQL table or the Access table (or both) does not contain a column called customer.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 34

Expert Comment

by:Sancler
Comment Utility
I know that this

>>
    For Each CustRow As DataRow In dtCustomerAccess.Rows
      Dim row As DataRow = dtCustomerSQL.NewRow

      row("Customer") = CustRow("Customer")
      dtCustomerSQL.Rows.Add(row)
    Next CustRow

<<

just reflects what was in the original post

>>
        For Each CustRow As DataRow In dsCustomerAccess.Tables("Customer").Rows

            r1 = dsOtherDataSet.Tables("Customer").NewRow
            r1("Customer") = CustRow("Customer")
            dsOtherDataSet.Tables("Customer").Rows.Add(r1)

        Next CustRow
<<

but I doubt very much that it is what is intended.  That is only putting one column's value in the new row.  I think what is wanted is something more like

      row.ItemArray = CustRow.ItemArray

That may still produce a problem if - as Chaosian's last post highlighted - the columns in the source and target tables have different names.  If they have different names, but are in the same order and with the same datatypes, then it should be achievbale with something like

     for i as integer = 0 to dsOtherDataSet.Tables("Customer").Columns.Count - 1
         row(i) = CustRow(i)
     next

Otherwise it might be necessary to map the source and target fields individually.

Roger
0
 
LVL 1

Author Comment

by:Eamon
Comment Utility
I understand you now.. I got totally mixed up. Thanks a million. Do you know anything about updating a value in the access database after I take it from their and put it into the sql server table. I have a field called disputed in access and this is a yes/no. Once sent to sql server, i want it to change from 0 to 1 in the access table so as when i go to update the records again it will only bring in ones that I dont have in my sql server database.

Gong to tackle it now.
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
Sure... build an update command for the access datatable (just like we did for the sql table).

Then iterate through your whole access datatable, set the value, and call the adapter update.
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
Comment Utility
Or, more quickly:

Dim update as New OledbCommand("UPDATE Customer SET Disputed = 1", conAccess)
conAccess.Open
update.ExectureNonQuery
conAccess.Close
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now