Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through dataset 1

Status
Not open for further replies.

slybitz

Technical User
Mar 25, 2005
113
US
I have created and filled a dataset with a single column of values. I want to take those values, looping through them one by one, and use those values as an input parameter in a stored procedure. I then want to execute that stored procedure using each of the dataset rows values as the parameter. How can I do this?


I know how to loop through the rows in the dataset (below) but I don't know how to pull the value from each row and use it as an input parameter. Any ideas?

Code:
            Dim dst As DataTable

            Dim dstr As DataRow

            For Each dst In ds.Tables 'ds is the dataset i already populated, let's say.

                For Each dstr In dst.Rows
                .?
                .?
                .?

Thanks for the help!!
 
Here's a basic example of how to do this.

Dim dst As DataTable

Dim dstr As DataRow

Sim cmd As SQLCommand 'assuming you are using SQL Server

cmd = New SQLCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = MyConn 'MyConn is a SQLConnection object
cmd.Commandtext = "MyStoredProc"

With cmd
.Parameters.Add("@MyParameter")
End With

For Each dst In ds.Tables

For Each dstr In dst.Rows
cmd.Parameters("@MyParameter").Value = dstr.Item("MyFieldName")
cmd.ExecuteNonQuery()
Next
Next

In this line - .Parameters.Add("@MyParameter") - you may need to include the parameter's type and size. Look in the help files for how to do this, or post back if you need more assistance.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Thanks for the help. I'll take a look at it in a bit and let you know.

I love the Homer quote by the way. Funny stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top