简体   繁体   中英

ado.net using same sqlcommand object to execute multiple commands

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

I have a Truncate table command which is executed first. 我有一个先执行的Truncate表命令。 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.

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.

you can do this, I believe...

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM