Reusing SQLCommand

  • Hi guys,

    How can I reuse one SQLCommand for inserting an x set of parameters?Like I created an SQLCommand which inserts Current Address details into the db.But if the user provide the Previous Addresses I wnat to reuse the same SQLCommand as it has the same parameters.How can I do this?

    ie: I have 4 parameters and 4 values for the current and 4 value sfor the previous address.

    Can I just creating 1 SQLCommand and adding one set of values to parameternames and then the other set of values to the same parameternames and executing the command?

    Thanks

    Ben

  • Let me make sure I understand your question, you have a sqlcommand with 4 parameters and you want to create the command once and use it multiple times with the paramters changing each time?  You can do this.  You can change the parameter.Value each time you call the sp.  Something like this:

    oCmd.Paramters("@address").Value = address

    oCmd.executenonquery

  • You can also use sp_execsql for this.

    You can also consider the option to run a single query that will update all rows in a single pass (assuming it's possible).

  • Hi Jack,

    Yes exactly, so I want to insert the provided current address details and if there is also the previous addresses fields (those have the same number of fields as current address, therefore the same parameter names), I also want to insert those fields at once, all with Transaction so if not all inserting is successful than none of them is inserted.

    I thought but was not sure about your way to do it.

    Thanks

    Ben

  • Benjamin,

    The ADO.NET has a tranascation object so can issue a Begin and a Commit/Rollback as appropriate.  If you google ADO.NET transaction you will find the syntax for it.

  • The Transaction object was clear for me to use, just was not sure how about the set of paramters for the same SQLCommand object.

    Thanks Jack

    Ben

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply