Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use OUTPUT parameters in ADO.Net c#
using System;
namespace OutputParamsInCSharp
{
class Program
{
static void Main(string[] args)
{
System.Data.SqlClient.SqlConnectionStringBuilder connectionBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
connectionBuilder.DataSource = "(localdb)\\v11.0";
connectionBuilder.InitialCatalog = "Fruit";
connectionBuilder.IntegratedSecurity = true;
using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(connectionBuilder.ConnectionString))
{
using (System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand("GetFruitByID", sqlConn))
{
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlConn.Open();
System.Data.SqlClient.SqlParameter paramId = new System.Data.SqlClient.SqlParameter("@id", System.Data.SqlDbType.Int);
paramId.Direction = System.Data.ParameterDirection.Input;
paramId.Value = 1;
sqlCmd.Parameters.Add(paramId);
System.Data.SqlClient.SqlParameter paramFruitName = new System.Data.SqlClient.SqlParameter("@FruitName", System.Data.SqlDbType.NVarChar);
paramFruitName.Direction = System.Data.ParameterDirection.Output;
paramFruitName.Size = 50;
sqlCmd.Parameters.Add(paramFruitName);
System.Data.SqlClient.SqlParameter paramFruitColor = new System.Data.SqlClient.SqlParameter("@FruitColor", System.Data.SqlDbType.NVarChar);
paramFruitColor.Direction = System.Data.ParameterDirection.Output;
paramFruitColor.Size = 50;
sqlCmd.Parameters.Add(paramFruitColor);
System.Data.SqlClient.SqlParameter paramFruitGrowsOn = new System.Data.SqlClient.SqlParameter("@FruitGrowsOn", System.Data.SqlDbType.Int);
paramFruitGrowsOn.Direction = System.Data.ParameterDirection.Output;
sqlCmd.Parameters.Add(paramFruitGrowsOn);
System.Data.SqlClient.SqlParameter paramFruitIsYummy = new System.Data.SqlClient.SqlParameter("@FruitIsYummy", System.Data.SqlDbType.Bit);
paramFruitIsYummy.Direction = System.Data.ParameterDirection.Output;
sqlCmd.Parameters.Add(paramFruitIsYummy);
sqlCmd.ExecuteNonQuery();
Console.WriteLine(paramFruitName.Value);
Console.WriteLine(paramFruitColor.Value);
Console.WriteLine(paramFruitGrowsOn.Value);
Console.WriteLine(paramFruitIsYummy.Value);
Console.ReadKey();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment