Output Parameters with Stored Proc ADO.NET
Here is a sample sproc that populates output parameters from the Northwind Products table:
Here is a sample sproc that populates output parameters
from the Northwind
Products table:
CREATE PROCEDURE CustOrderOne
@CustomerID
nchar(5),
@ProductName varchar(50) output,
@Quantity int output
AS
SELECT TOP 1 @ProductName=PRODUCTNAME, @Quantity =quantity
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE
C.CustomerID = @CustomerID
AND C.CustomerID = O.CustomerID AND O.OrderID
= OD.OrderID AND OD.ProductID = P.ProductID
And here is an example
of some C# code to return and display the output parameters:
using
System;
using System.Data;
using System.Data.SqlClient;
namespace
OutPutParms
{
class OutputParams
{
[STAThread]
static
void Main(string[] args)
{
using( SqlConnection cn = new SqlConnection("server=(local);Database=Northwind;user
id=sa;password=;"))
{
SqlCommand cmd = new SqlCommand("CustOrderOne",
cn);
cmd.CommandType=CommandType.StoredProcedure ;
SqlParameter
parm=new SqlParameter("@CustomerID",SqlDbType.NChar) ;
parm.Value="ALFKI";
parm.Direction =ParameterDirection.Input ;
cmd.Parameters.Add(parm);
SqlParameter parm2=new SqlParameter("@ProductName",SqlDbType.VarChar);
parm2.Size=50;
parm2.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm2);
SqlParameter parm3=new SqlParameter("@Quantity",SqlDbType.Int);
parm3.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parm3);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
Console.WriteLine(cmd.Parameters["@ProductName"].Value);
Console.WriteLine(cmd.Parameters["@Quantity"].Value.ToString());
Console.ReadLine();
}
}
}
}
Submission Date: 9/23/2005 3:18:26 PM
Submitted
By: Peter Bromberg
My Home Page: http://www.eggheadcafe.com
By Peter Bromberg Popularity (1968 Views)