Solved

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

Posted on 2006-07-18
7
193 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
ID: 17129309
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
ID: 17129459
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
ID: 17129473
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Expert Comment

by:Sancler
ID: 17130171
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
ID: 17130319
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
ID: 17130389
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
ID: 17130420
Or, more quickly:

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expando 4 46
Round a string to two digits 12 30
Visual Studio .Net 2012 and Crystal Reports 9 43
VB.Net How to Exit Sub - Exit Form??? 5 47
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

15 Experts available now in Live!

Get 1:1 Help Now