程序段中连续存在多个存储过程 [英] multiple stored procedure in succession in a program segment

查看:77
本文介绍了程序段中连续存在多个存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有注释来源的程序段,但我想在程序中拆分



功能,以便我有 3个存储过程代替



当前的一个。所以在按下按钮 3存储过程将连续执行






请注意,选择按钮后,所有3个存储过程的发送参数都是相同的。





如何做到这一点?









 尝试 
{
sqlCon.Open();
SqlCommand SqlCmd = new SqlCommand( sp_ProcessSimpleConvent ,sqlCon);
SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

// 创建并提供输出参数

SqlCmd.Parameters.AddWithValue( @ CMONTH,SqlDbType.Int).Value = txt_Period。文本;
SqlCmd.Parameters.AddWithValue( @ CYEAR,SqlDbType.Int).Value = txt_Year.Text;



SqlCmd.Parameters.Add( @ RETURN ,System.Data.SqlDbType.VarChar, 12 );
SqlCmd.Parameters [ @ RETURN]。Direction = System.Data.ParameterDirection。输出;

SqlCmd.ExecuteNonQuery();









假设要执行的存储过程是



1. sp_simple



2. sp_Convent



3. sp_None





请注意发送参数对于在选择按钮时继续运行的所有3个存储过程是相同的



谢谢

解决方案

hi,



检查下面的代码我每次执行一个程序时都会在标签上打印一条消息。



 SqlConnection conn = new SqlConnection (你的连接字符串); 
conn.Open();
string [] proc = {sp_simple,sp_Convent,sp_None};
for(int i = 0; i< proc.Length; i ++)
{
SqlCommand cmd = new SqlCommand(proc [i],conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ CMONTH,txt_Period.Text);
cmd.Parameters.AddWithValue(@@ CYEAR,txt_Year.Text);
cmd.Parameters.AddWithValue(@ RETURN,12);
cmd.Parameters [@ RETURN]。Direction = System.Data.ParameterDirection.Output;
cmd.ExecuteNonQuery();
label2.Text + = proc [i] +执行。\ n;
}
conn.Close();







但它更好在一个程序中编写所有功能更好


我没有测试这个代码,因为今天我的机器上没有VS,请先尝试一下这个想法。然后编写自己的代码更好。



 main()
{
线程t1 = new 线程( new ThreadStart(ThreadProc1));
线程t2 = new 线程( new ThreadStart(ThreadProc2));
线程t3 = new 线程( new ThreadStart(ThreadProc3));
t1.Start();
t2.Start();
t3.Start();
}

void ThreadProc1()
{
使用(SqlConnection connection = new SqlConnection(
yourConnectionString))
{
SqlCommand command = new SqlCommand( sp_simple,connection);
command.Connection.Open();
command.Connection.Open.CommandType = CommandType.StoredProcedure;
SqlParameter par1 = new SqlParameter( @ CMONTHID,SqlDbType.NVarChar);
SqlParameter par2 = new SqlParameter( @ CYEAR,SqlDbType.NVarChar);
command.Parameters.Add(par1,txt_Period.Text);
command.Parameters.Add(par2,txt_Year.Text);
command.ExecuteNonQuery();
}
}

void ThreadProc2()
{
using (SqlConnection connection = new SqlConnection(
yourConnectionString))
{
SqlCommand command = new SqlCommand( sp_Convent,连接);
// 与ThreadProc1相同
}
}
void ThreadProc3()
{
使用(SqlConnection connection = new SqlConnection(
yourConnectionString))
{
SqlCommand command = new SqlCommand( sp_None,connection);

// 与ThreadProc1相同
}
}


I have a program segment with the undernoted source, however I want to split the

functionaliy in the program so that I will have 3 stored procedures in place of the

current one. so that on pressing the button the 3 stored procedures will be

performed in succession.

Please note the sending parameters are the same for all the 3 stored procedures to be run in succession on the selection of a button.


How do I accomplish this ?




try
{
    sqlCon.Open();
    SqlCommand SqlCmd = new SqlCommand("sp_ProcessSimpleConvent", sqlCon);
    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
    
    //Create and supply the output parameters
    
    SqlCmd.Parameters.AddWithValue("@CMONTH", SqlDbType.Int).Value = txt_Period.Text;
    SqlCmd.Parameters.AddWithValue("@CYEAR", SqlDbType.Int).Value  = txt_Year.Text;
    
    
    
    SqlCmd.Parameters.Add("@RETURN", System.Data.SqlDbType.VarChar, 12);
    SqlCmd.Parameters["@RETURN"].Direction = System.Data.ParameterDirection.Output;
    
    SqlCmd.ExecuteNonQuery();





Assuming the stored procedures to be performed are

1. sp_simple

2. sp_Convent

3. sp_None


Please note the sending parameters are the same for all the 3 stored procedures to be run in succession on the selection of a button.

Thanks

解决方案

hi,

check below code i have printed a message on label each time when a procedure gets executed.

SqlConnection conn = new SqlConnection(your connection string);
        conn.Open();
        string[] proc = { "sp_simple", "sp_Convent", "sp_None" };
        for (int i = 0; i < proc.Length; i++)
        {
            SqlCommand cmd = new SqlCommand(proc[i], conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CMONTH", txt_Period.Text);
            cmd.Parameters.AddWithValue("@@CYEAR", txt_Year.Text);
            cmd.Parameters.AddWithValue("@RETURN", 12);
            cmd.Parameters["@RETURN"].Direction = System.Data.ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            label2.Text += proc[i] + "is executed.\n";
        }
        conn.Close();




but its better to write all the functionalities with in one procedure is better


I didnt Test this code, Because today I dont have VS on my mechine, Please try to get the idea first. And then write your own code is better.

main()
{
Thread t1 = new Thread(new ThreadStart(ThreadProc1));
Thread t2 = new Thread(new ThreadStart(ThreadProc2));
Thread t3 = new Thread(new ThreadStart(ThreadProc3));
t1.Start();
t2.Start();
t3.Start();
}

void ThreadProc1()
{
using (SqlConnection connection = new SqlConnection(
               yourConnectionString))
    {
        SqlCommand command = new SqlCommand("sp_simple", connection);
        command.Connection.Open();
        command.Connection.Open.CommandType = CommandType.StoredProcedure;
        SqlParameter par1= new SqlParameter("@CMONTHID", SqlDbType.NVarChar);
        SqlParameter par2 = new SqlParameter("@CYEAR", SqlDbType.NVarChar);
        command.Parameters.Add(par1,txt_Period.Text);
        command.Parameters.Add(par2,txt_Year.Text);
        command.ExecuteNonQuery();
    }
}

void ThreadProc2()
{
using (SqlConnection connection = new SqlConnection(
               yourConnectionString))
    {
        SqlCommand command = new SqlCommand("sp_Convent", connection);
        //Same As ThreadProc1
    }
}
void ThreadProc3()
{
using (SqlConnection connection = new SqlConnection(
               yourConnectionString))
    {
        SqlCommand command = new SqlCommand("sp_None", connection);

        //Same As ThreadProc1
    }
}


这篇关于程序段中连续存在多个存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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