简体   繁体   English

ado.net使用相同的sqlcommand对象来执行多个命令

[英]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. 可以告诉任何人使用相同的SqlCommand对象在.net应用程序中执行多个命令。

Situation: 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. 然后我将执行SqlBulkcopy操作然后我希望相同的命令对象执行另一个存储过程,它将执行一些更新或将数据移动到不同的表。

I dont want to create a new Command object. 我不想创建一个新的Command对象。

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. 您可以使用相同的SqlCommand对象来执行更多命令。 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 如果您需要在不同的批次中运行查询段,您是否可以在它们之间的一个命令文本“GO”语句中包含所有操作,您也可以使用此事务

TRUNCATE TABLE X;
GO

/* do SqlBulkcopy code*/
GO

/* EXECUTE STORED PROC*/
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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