Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Copy Record(s) from 1 Database to Another Database with the same Tables

Copy Record(s) from 1 Database to Another Database with the same Tables

Scheduled Pinned Locked Moved Visual Basic
helpdatabasequestionannouncement
2 Posts 2 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crmfghtr
    wrote on last edited by
    #1

    Hi Everyone, I hope I'm posting this in the right place. I'm using VistaDb version 6.2 I have a Main Database with several tables Like Clients, Inventory, etc. I have a Backup Database with the same table(s) and structure as the Main database. I'm trying to copy 1 or more records from the Backup Database, like the Inventory Table to the Main Database Inventory Table. The code below is inserting records from the Main database inventory table, and not from the backup database. Do I need to do this by First selecting the data and placing it in a tmp table then insert the records from the tmp table? Any help would be appreciated. Here is the code I have:

    Private Sub TransferInventory()
    
        Dim MyFile As String = TreeListTransfer.FocusedNode(0).ToString
        Dim VdbConn1 As String = "Data Source=" & Application.StartupPath & "\\Backup\\" & MyFile & ".vdb6"
        Try
            'Count the files in the Listbox
            For i As Integer = 0 To LstFiles.Items.Count - 1
                LstFiles.SelectedIndex = i
                FileId = CInt(LstFiles.Text)
    
                'Add the Contents to the Database
                Using conn As New VistaDBConnection(VdbConn)
                    conn.Open()
                    StrSql = "INSERT INTO Inventory(ClientId, Category, Product, PartNo, PurchaseDate, Unit, UnitPrice, InStock, OnOrder, Photo) 
                          SELECT ClientId, Category, Product, PartNo, PurchaseDate, Unit, UnitPrice, InStock, OnOrder, Photo
                          FROM dbo.Inventory
                          WHERE (InventoryId = @InventoryId)"
    
                    Using cmd As New VistaDBCommand(StrSql, conn)
                        With cmd.Parameters
                            .AddWithValue("@InventoryId", FileId)
                        End With
                        'Execute the Statement
    
                        cmd.ExecuteNonQuery()
                        'Close the Connection
                        conn.Close()
                    End Using
                End Using
    
            Next
            MessageBox.Show("Transfer successfully completed on the 'Inventory' table", "Transfer Inventory Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Transfer Inventory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        Finally
    
        End Try
    
    End Sub
    

    Thanks in Advance.

    V 1 Reply Last reply
    0
    • C crmfghtr

      Hi Everyone, I hope I'm posting this in the right place. I'm using VistaDb version 6.2 I have a Main Database with several tables Like Clients, Inventory, etc. I have a Backup Database with the same table(s) and structure as the Main database. I'm trying to copy 1 or more records from the Backup Database, like the Inventory Table to the Main Database Inventory Table. The code below is inserting records from the Main database inventory table, and not from the backup database. Do I need to do this by First selecting the data and placing it in a tmp table then insert the records from the tmp table? Any help would be appreciated. Here is the code I have:

      Private Sub TransferInventory()
      
          Dim MyFile As String = TreeListTransfer.FocusedNode(0).ToString
          Dim VdbConn1 As String = "Data Source=" & Application.StartupPath & "\\Backup\\" & MyFile & ".vdb6"
          Try
              'Count the files in the Listbox
              For i As Integer = 0 To LstFiles.Items.Count - 1
                  LstFiles.SelectedIndex = i
                  FileId = CInt(LstFiles.Text)
      
                  'Add the Contents to the Database
                  Using conn As New VistaDBConnection(VdbConn)
                      conn.Open()
                      StrSql = "INSERT INTO Inventory(ClientId, Category, Product, PartNo, PurchaseDate, Unit, UnitPrice, InStock, OnOrder, Photo) 
                            SELECT ClientId, Category, Product, PartNo, PurchaseDate, Unit, UnitPrice, InStock, OnOrder, Photo
                            FROM dbo.Inventory
                            WHERE (InventoryId = @InventoryId)"
      
                      Using cmd As New VistaDBCommand(StrSql, conn)
                          With cmd.Parameters
                              .AddWithValue("@InventoryId", FileId)
                          End With
                          'Execute the Statement
      
                          cmd.ExecuteNonQuery()
                          'Close the Connection
                          conn.Close()
                      End Using
                  End Using
      
              Next
              MessageBox.Show("Transfer successfully completed on the 'Inventory' table", "Transfer Inventory Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
          Catch ex As Exception
              MessageBox.Show(ex.Message, "Transfer Inventory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
              Return
          Finally
      
          End Try
      
      End Sub
      

      Thanks in Advance.

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      crmfghtr wrote:

      he code below is inserting records from the Main database inventory table, and not from the backup database

      It is because you don't mention in your SQL query from/to what database you select/insert the data. See [sql - How to insert table values from one database to another database? - Stack Overflow](https://stackoverflow.com/questions/3502269/how-to-insert-table-values-from-one-database-to-another-database)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups