如何在业务逻辑中使用输出参数传递给sp [英] how to use out put parameter in business logic to pass to sp

查看:58
本文介绍了如何在业务逻辑中使用输出参数传递给sp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用输入和输出参数编写了一个sp,如何在c#

i have written a sp with input and out put parameter ,how can i call that sp from my business logic in c#

推荐答案

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();



请参阅:

如何:使用ADO.NET和Visual C#.NET调用参数化存储过程 [ ^ ]

如何:执行返回行的存储过程 [ ^ ]



-KR


Refer this:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
How to: Execute a Stored Procedure that Returns Rows[^]

-KR


执行以下步骤(添加其他输入parmas(如果有的话): -



Do the following steps (add additional input parmas if any):-

using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True"))
using (SqlCommand cmd = new SqlCommand("SPname", con))
{
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@opparam1", SqlDbType.Int));
    cmd.Parameters["@opparam1"].Direction = ParameterDirection.Output;

    con.Open();
    cmd.ExecuteNonQuery();
    int outputvalue = (int)cmd.Parameters["@opparam1"].Value;
    con.Close();
}


公共类b_GroupTest

{



SqlHelper sh = new SqlHelper();

public DataSet TestQuestions(GroupTest Gt)

{

DataSet ds = new DataSet();

尝试

{

SqlParameter [] param = {

new SqlParameter(@ TotalQuestions,Gt.NoofQuestions ),

新的SqlParameter(@ Companyid,Gt.comp),



};



返回ds = sh.ExecuteDataset(dbo.Sp_GetQuestions,param);



}

catch(例外情况ex)

{}

返回ds;



}

}



这里我有pass2参数一个输入作为公司和一个参数bu我得到输出值。
public class b_GroupTest
{

SqlHelper sh = new SqlHelper();
public DataSet TestQuestions(GroupTest Gt)
{
DataSet ds = new DataSet();
try
{
SqlParameter[] param ={
new SqlParameter("@TotalQuestions",Gt.NoofQuestions),
new SqlParameter("@Companyid",Gt.comp),

};

return ds = sh.ExecuteDataset("dbo.Sp_GetQuestions", param);

}
catch (Exception ex)
{ }
return ds;

}
}

here i have pass2 parameter one input as company and a parameter bu which i get the output value .


这篇关于如何在业务逻辑中使用输出参数传递给sp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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