Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have written a sp with input and out put parameter ,how can i call that sp from my business logic in c#
Posted

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();

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
 
Share this answer
 
Do the following steps (add additional input parmas if any):-

C#
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();
}
 
Share this answer
 
Comments
Member 11970398 18-Nov-15 1:42am    
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 @testid which i get the output value .
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 .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900