• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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
0
soccerman777
Asked:
soccerman777
  • 6
  • 4
  • 2
  • +1
1 Solution
 
the_bachelorCommented:
once you have your dataset, you can loop through it by doing
for each row as DataRow in myDataset.Tables(0).Rows
   'Write you code to perform inserts
   'Access column using row.Item("ColumnName")
next

helps?
0
 
Bane83Commented:
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
0
 
soccerman777Author Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Bane83Commented:
Ah, yes, you do.  You can use the method that the_bachelor mentioned
0
 
soccerman777Author Commented:
bachelor,

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

0
 
soccerman777Author Commented:
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
0
 
Bane83Commented:
You can use row("ColumnName") replacing ColumnName with whatever the columns name is.
0
 
the_bachelorCommented:
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 & "')"
0
 
samtran0331Commented:
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

0
 
soccerman777Author Commented:
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.
0
 
Bane83Commented:
Sorry, you accepted the wrong person for the solution.  Hopefully you can undo that.
0
 
soccerman777Author Commented:
Sorry You put that in your comments in your first soultion.  I should have read them.
0
 
soccerman777Author Commented:
I asked that the points for this soultion would go to the _bachelor
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now