如何单击执行多个存储过程? [英] How Do I Execute Multiple Stored Procedures On Single Click ?

查看:113
本文介绍了如何单击执行多个存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同的ID多次运行此命令



执行dbs_sp_applyrule2_helo + id1

执行dbs_sp_applyrule2_helo + id2

执行dbs_sp_applyrule2_helo + id3

执行dbs_sp_applyrule2_helo + id4









执行rbs_sp_applyrule2_mtbc + id50



注意:ids是一个总是不同的数字

试试这个:

 使用(SqlConnection connection =  new  SqlConnection( 您的连接字符串在这里)) 
使用(SqlCommand command = new SqlCommand( dbs_sp_applyrule2_helo,connection))
{
command.CommandType = CommandType.StoredPro cedure;

SqlParameter idParameter = command.Parameters.Add( @ ID, SqlDbType.Int);

connection.Open();

foreach int id in listOfIdsToUse)
{
idParameter.Value = id;
command.ExecuteNonQuery();
}
}


选择你喜欢的:

  foreach  int  id  in  idCollection){
// 您的当前ID的执行代码
}

for int i = 1 to 50 ){
// 您的EXEC代码当前ID(i)
}

int i = 0 ;
while (++ i > 50 ){
// 您的EXEC cide与当前ID(i)
}


Quote:





请在for或foreach循环中创建动态查询并一次性执行....



您需要创建一个新的存储过程,该过程将使此查询成为输入参数。



希望你有概念..


i want to run this command several times with different ids

Exec dbs_sp_applyrule2_helo + id1
Exec dbs_sp_applyrule2_helo + id2
Exec dbs_sp_applyrule2_helo + id3
Exec dbs_sp_applyrule2_helo + id4
.
.
.
.
Exec rbs_sp_applyrule2_mtbc + id50

Note: ids is a number always different

解决方案

Try this:

using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
using (SqlCommand command = new SqlCommand("dbs_sp_applyrule2_helo", connection))
{
    command.CommandType = CommandType.StoredProcedure;
    
    SqlParameter idParameter = command.Parameters.Add("@ID", SqlDbType.Int);
    
    connection.Open();
    
    foreach (int id in listOfIdsToUse)
    {
        idParameter.Value = id;
        command.ExecuteNonQuery();
    }
}


Pick your favorite:

foreach(int id in idCollection) {
// your Exec code with current ID
}

for (int i = 1 to 50) {
// your EXEC code with current ID (i)
}

int i = 0;
while (++i > 50) {
// your EXEC cide with current ID (i)
}


Quote:

Hi,

Please create a dynamic query in for or foreach loop and execute it in one hit....

for that you need to create a new stored procedure which will take this query a input parameter.

hope you got concept..


这篇关于如何单击执行多个存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆