Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17



Posted on 2006-11-10
Medium Priority
Last Modified: 2008-07-27
i have 2 db same structre and i want to copy the data of table1 from db1 to db2
but i dont want to include the first field(key field and dont care about it)  

i have done this in

    Private Sub UpdateData(ByVal SelSql As String)

        Dim sourceSQL As String = SelSql
        Dim targetSQL As String = SelSql
        Dim targetDA As New OleDbDataAdapter(targetSQL, CON1)
        Dim cb As New OleDbCommandBuilder(targetDA)
        Dim targetCommand As OleDbCommand = cb.GetInsertCommand
        Dim cmdText As String = targetCommand.CommandText

        If CON2.State = Data.ConnectionState.Closed Then CON2.Open()
        If targetCommand.Connection.State = Data.ConnectionState.Closed Then targetCommand.Connection.Open()

        cmdText = cmdText.Replace("( ", "( [")
        cmdText = cmdText.Replace(" , ", "] , [")
        cmdText = cmdText.Replace(" )", "] )")
        cmdText = cmdText.Replace("[?]", "?")

        targetCommand.CommandText = cmdText
        Dim sourceCommand As New OleDbCommand(sourceSQL, CON2)
        Dim sourceReader As OleDbDataReader
        sourceReader = sourceCommand.ExecuteReader()

        Me.Text = 0
        While sourceReader.Read()
            For i As Integer = 1 To sourceReader.FieldCount - 1
                targetCommand.Parameters(i - 1).Value = sourceReader(i)
            Me.Text = Me.Text + 1
        End While

    End Sub 

this works but is there any better way this takes to much time


Question by:rockas1982
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4
LVL 24

Expert Comment

by:Jeff Certain
ID: 17916083
The easy way to create a make-table query and execute it.

What database are you targeting?
LVL 24

Expert Comment

ID: 17916111
If you only need to copy the data once, you could export it to a flat file (e.g. CSV), then bulk import the flat file into the other DB.
LVL 24

Expert Comment

by:Jeff Certain
ID: 17916123
Code looks something like this:

Using conn as New OleDbConnection(connectionString)
  Dim sql as String = "SELECT <fieldList>  INTO <destinationTable> FROM <sourceTables>"
  Using cmd as New OleDbCommand(sql,conn)
  End Using
End Using

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 17916280
thnx for fast replay

i'm using access 2003 with 2003

i'm new in sql commands and i saw something that might work the "IN" command .. can this help me ?

the sample i saw is this

strSQL = "INSERT INTO Pricebook  SELECT * FROM Pricebook IN 'K:\PriceBk\PriceBook.mdb'"

i want to give the filepathh from sourcefile and targetfile
Can i connect to my target database and make an insert query asking data from onother database "insert into table1 (all fileds except the first) select table1 (all fileds except the first) IN (filepathAndName) (where shop = "shop1")
do tou think this may work

P.S. is there any way i can add progressbar into this so user can see how long this will take(not important)

sorry for my english(Greece)

LVL 34

Expert Comment

ID: 17916281

Hi Jeff ;-).  Long time no speak.

Can you do that from one DB to another?  Won't you need two different connections?


Author Comment

ID: 17916322
I hoped that sql will call the second database with the "IN" command ... don't really know
LVL 24

Expert Comment

by:Jeff Certain
ID: 17916372

Shoot... you almost caught me...

The sneaky here is that with an enterprise level database (read SQL Server), you can use fully-qualified table names to work across multiple registered databases. I missed the MDB part.

Dimitri... let me post a pointer for you....the Acces gurus can probably help out on this one...
LVL 24

Expert Comment

by:Jeff Certain
ID: 17916407
Dimitri.... I'll do one better....

Create the query in Access using the query builder. Click the button in the upper left until it says "SQL"... cut-and-paste this into your VB app as the SQL statement for your command (fixing any quotes, as required)

This way, you can test the SQL statement to make sure it does what you want... and the make-table wizard lets you specify the other database to use.
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17916439
Looks like you're already along the right lines.
You just have to specify the fields you do want (to eliminate the fields you don't).

strSQL = "INSERT INTO Pricebook (Field2, Field3, Field26) SELECT Field2, Field3, Field26 FROM Pricebook IN 'K:\PriceBk\PriceBook.mdb'"
strSQL = "INSERT INTO Pricebook (Field2, Field3, Field26) SELECT Field2, Field3, Field26 FROM [K:\PriceBk\PriceBook.mdb].Pricebook"

Not dissimilar to using a linked server - considering that mdb's are file based - you just specify the file.
Executed with a connection to the db you want to receive the records of course.
LVL 26

Expert Comment

ID: 17916462
basic ssyntax is
Select * into tablename in '\path\database.mdb' from tablename
LVL 26

Expert Comment

ID: 17916471
isn't insert into an append?
LVL 44

Assisted Solution

by:Leigh Purvis
Leigh Purvis earned 400 total points
ID: 17916491
Yeah - an append.
Select Into would create a new table - but I understood the question to imply the two db's already had a similar structure.

(Oh, and no rockas1982 there's no exact way to get a progress meter, as the query engine's execution can't be hooked into).
LVL 26

Assisted Solution

jerryb30 earned 800 total points
ID: 17916498
right you are.
LVL 44

Accepted Solution

GRayL earned 800 total points
ID: 17916748
Something like this?

Select fld1, fld2, fl3 INTO myNewTable IN "c:\Data\AccessData\myOtherMDB.mdb" FROM myTable;

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

715 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