Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Reusing SQLCommand Expand / Collapse
Author
Message
Posted Monday, September 24, 2007 7:50 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, February 1, 2011 8:57 AM
Points: 88, Visits: 41
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
Post #402021
Posted Monday, September 24, 2007 2:51 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 8:26 AM
Points: 10,381, Visits: 13,436
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



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #402252
Posted Monday, September 24, 2007 3:13 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, November 27, 2014 4:23 AM
Points: 20,584, Visits: 9,624
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).

Post #402261
Posted Tuesday, September 25, 2007 4:15 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, February 1, 2011 8:57 AM
Points: 88, Visits: 41
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
Post #402471
Posted Tuesday, September 25, 2007 6:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 8:26 AM
Points: 10,381, Visits: 13,436
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.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #402505
Posted Tuesday, September 25, 2007 7:04 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, February 1, 2011 8:57 AM
Points: 88, Visits: 41
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
Post #402537
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse