Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Command with Parameters, Loop?

Posted on 2006-05-01
3
Medium Priority
?
490 Views
Last Modified: 2012-08-13
Hi,

I am using Several SQL Commands, all of which have parameters:

            Command1.Parameters.Add("@param1", System.Data.SqlDbType.Varchar)
            Command1.Parameters("@param1").Value = "test"


I want to be able to run these commands more than once by using a loop....First pass thru runs fine, but then when it tries to loop I get the following error:

"The variable name '@param1' has already been declared. Variable names must be unique within a query batch or stored procedure."


How can I reset these so I can loop thru and run the same commands with the parameters reset ? Thanks for all your help !
0
Comment
Question by:kasowitz
3 Comments
 
LVL 34

Accepted Solution

by:
flavo earned 2000 total points
ID: 16582726
           Command1.Parameters.Clear
            Command1.Parameters.Add("@param1", System.Data.SqlDbType.Varchar)
            Command1.Parameters("@param1").Value = "test"

Dave
0
 

Author Comment

by:kasowitz
ID: 16582753
thanks
0
 
LVL 10

Expert Comment

by:ericwong27
ID: 16582770

Dim C_Command As SqlCommand
For i = 0 to 10
            sql = "DELETE FROM Table1 WHERE (Code = @Code)"
            C_Command = New SqlCommand(sql, C_connection)
            C_Command.Parameters.Add("@Code", SqlDbType.VarChar, 16).Value = i.ToString
            C_Command.ExecuteNonQuery()
Next

           
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question