Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Read records from sql statement and insert those records into a sql table in vb.net

Hi all.

I want to read records from a sql statement and then insert those records into a sql server 2008 table. I was thinking of using SqlDataReader to do so but don't know how to get it to work, below is some code I've started to work on.

Please let me know how I can finish it off, or if I'm doing it incorrectly then what is the correct way to do so.

Thank you in advance.

Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Dim  itemnumber1 As String
        Dim qtyordered, qtyshipped, qtybackordered As Decimal

con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

cmd.CommandText = "SELECT ItemNumber, QtyOrdered, QtyShipped, QtyBackordered FROM myTableA t1 INNER JOIN myTableB t2 ON t1.InvoiceNumber = t2.InvoiceNumber WHERE (t2.SkipPrintingOfThisComponent <> 'Y') AND (t1.SalesOrderNumber = @SalesOrder3)"
        cmd.Parameters.AddWithValue("@SalesOrder3", txtSalesOrder.Text)

While dr.Read()
            itemnumber1 = dr("ItemNumber")
            qtyordered = dr("QtyOrdered")
            qtyshipped = dr("QtyShipped")
            qtybackordered = dr("QtyBackordered")

            cmd.CommandText = "INSERT INTO tblOrderPicking_Detail(  SalesOrder,ItemNumber,QtyOrderedEaches,QtyShippedEaches,QtyBackorderedEaches) VALUES (@SalesOrder4,@ItemNumber1,@QtyOrderedEaches,@QtyShippedEaches,@QtyBackorderedEaches )"

   
            cmd.Parameters.AddWithValue("@SalesOrder4", txtSalesOrder.Text)
            cmd.Parameters.AddWithValue("@ItemNumber1", itemnumber1)
            cmd.Parameters.AddWithValue("@QtyOrderedEaches", qtyordered)
            cmd.Parameters.AddWithValue("@QtyShippedEaches", qtyshipped)
            cmd.Parameters.AddWithValue("@QtyBackorderedEaches", qtybackordered)

            cmd.ExecuteNonQuery()
        End While
        dr.Close()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

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
SOLUTION
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

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