Search content within the blog

Friday, April 17, 2009

InputOutput Parameters in SQL Server Stored Procedures and ADO.net

CREATE PROCEDURE Increment (@X INT OUTPUT) AS SET @X = @X + 1

And test it using the following code.

using System;
using System.Data;
using System.Data.SqlClient;

public class InputOutputParameters
{
public int Increment(int x)
{
SqlConnection connection = new SqlConnection(@"your connection...");
SqlCommand command = connection.CreateCommand();
command.CommandText = "Increment";
command.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter("@X", SqlDbType.Int, 4);
parameter.Direction = ParameterDirection.InputOutput;
parameter.Value = x;
command.Parameters.Add(parameter);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
return (int) parameter.Value;
}
public static void Main()
{
Console.WriteLine( new InputOutputParameters().Increment(3).ToString() );
}
}

No comments:

Post a Comment