Link to home
Start Free TrialLog in
Avatar of soccerman777
soccerman777

asked on

Really simple question about looping through a querry

I have the following querry
Dim strSQLLoop As String
        strSQLLoop = "SELECT description,unitPrice,extendedAmount,vehicleNumber,qty from imtbl_templineitem"
        Dim DBCommandLoop As New SqlDataAdapter(strSQLLoop, "myconnectionstring")
        Dim rs As New DataSet
        DBCommandLoop.Fill(rs)


Please give me a example of how I can loop through this record set and insert into another table with each iteration
ASKER CERTIFIED SOLUTION
Avatar of the_bachelor
the_bachelor

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
Could you just explain why you need to loop through each one and then copy each row to a new table?

The reason I ask this is that you can make the copy without doing the loop:
Dim dt as DataTable = rs.Tables(0).Copy
Avatar of soccerman777
soccerman777

ASKER

I am inserting each row in a temp table into a diferent live table. The table structures are different so I assumed I have to loop through each row and then insert the currect row into the table
Ah, yes, you do.  You can use the method that the_bachelor mentioned
bachelor,

How do I reference the data fields from the querry in the loop. Please give me a example of the insertsql statement.

I do now how to do a insert I jjust need a example of how to reference the var from the recordset at the current row
You can use row("ColumnName") replacing ColumnName with whatever the columns name is.
sure i tried to accomplish that on my 1st post
you can build a sql statement like the folowing inside the for loop

Dim sSql as String = "INSERT INTO myNewTable (col1, col2) VALUES ('" & row("columnName1").ToString & "', " '" & row("ColumnName2").ToString & "')"
Keep in mind that with this method, for every row in the temp table, you are executing a separate insert statement and for, say 1000 rows, that's 1000 separate db calls from your code...

If your goal is simply to update one table with data from another table, you can do it all in sql with something like:
INSERT INTO RealTable(RealField1,RealField2,RealField3,RealField4,RealField5)
SELECT description,unitPrice,extendedAmount,vehicleNumber,qty from imtbl_templineitem

Which will insert into "RealTable" a row for every row that the select query returns...same as coding a row by row insert through code except exponentially more efficient because the database server is handling it....

While it would best be done in a stored procedure...you can probably do the below:


Dim StrSQL As String = "INSERT INTO RealTable(RealField1,RealField2,RealField3,RealField4,RealField5) SELECT description,unitPrice,extendedAmount,vehicleNumber,qty from imtbl_templineitem" 
           Using MyConn As New SqlConnection("myconnectionstring")
                MyConn.Open()
                Dim MyTrans As SqlTransaction = MyConn.BeginTransaction
                Dim MyCmd As New SqlCommand(StrSQL, MyConn, MyTrans)
                Try
                    MyCmd.ExecuteNonQuery()
                    MyTrans.Commit()
                Catch ex As Exception
                    MyTrans.Rollback()
                Finally
                    If MyConn.State = ConnectionState.Open Then MyConn.Close()
                End Try
            End Using

Open in new window

samtran0331 I like your method but I have already used the simple soultion. I will post a question in a min asking how to do this in one sql statment because I think it is only fair you sould get points for giveing me such a nice soultion.
Sorry, you accepted the wrong person for the solution.  Hopefully you can undo that.
Sorry You put that in your comments in your first soultion.  I should have read them.
I asked that the points for this soultion would go to the _bachelor