2

Can any one tell about Using same SqlCommand object to execute multiple commands in .net applicaton.

Situation: I have a Truncate table command which is executed first. then I will perform SqlBulkcopy operations then i want the same command object to execute another stored procedure which will perform some updation or moving the data to different tables.

I dont want to create a new Command object.

One more thing all the three operations are in a transaction.

Thanks in advance.

4 Answers 4

5

You don't have to create a new object. Just specify a new

command.CommandText = "SELECT FROM WHERE...";

with the neccesary parameters before executing each command.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use the same SqlCommand object to execute more commands. Just be sure to reset the following properties to appropriate values

Comments

2

you can do this, I believe...

SqlCommand command = new SqlCommand();
command.CommandText = 
    "SELECT FROM WHERE...; SELECT FROM WHERE...; SELECT FROM WHERE...;";

Comments

0

Can't you include all operations in one command text "GO" statement between them if you need query segments to be run in separate batches you can use the transactions also with this

TRUNCATE TABLE X;
GO

/* do SqlBulkcopy code*/
GO

/* EXECUTE STORED PROC*/
GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.