soccerman777
asked on
Really simple question about looping through a querry
I have the following querry
Dim strSQLLoop As String
strSQLLoop = "SELECT description,unitPrice,exte ndedAmount ,vehicleNu mber,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
Dim strSQLLoop As String
strSQLLoop = "SELECT description,unitPrice,exte
Dim DBCommandLoop As New SqlDataAdapter(strSQLLoop,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
bachelor,
How do I reference the data fields from the querry in the loop. Please give me a example of the insertsql statement.
How do I reference the data fields from the querry in the loop. Please give me a example of the insertsql statement.
ASKER
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").ToStrin g & "', " '" & row("ColumnName2").ToStrin g & "')"
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").ToStrin
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,RealF ield2,Real Field3,Rea lField4,Re alField5)
SELECT description,unitPrice,exte ndedAmount ,vehicleNu mber,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:
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,RealF
SELECT description,unitPrice,exte
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
ASKER
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.
ASKER
Sorry You put that in your comments in your first soultion. I should have read them.
ASKER
I asked that the points for this soultion would go to the _bachelor
The reason I ask this is that you can make the copy without doing the loop:
Dim dt as DataTable = rs.Tables(0).Copy