如何将此nvarchar返回给C# [英] how do return this nvarchar to c#

查看:85
本文介绍了如何将此nvarchar返回给C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有此存储过程:

hi all

i have this Stored Procedure :

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[TOP_Returner]
    (@number [bigint],
     @return_top nvarchar(200) output
    )
AS
declare @top_name nvarchar(200)

if @number=1
begin
select  top (1) @top_name=[product_name] From View_Product_4_TopBest
set @return_top=@top_name
return @return_top
end

if @number=2
begin
select top (1) @top_name=[color_name] From View_Color_2_TopBest
set @return_top=@top_name
return @return_top
end

if @number=3
begin
select top (1) @top_name=[model_name] From View_Model_2_TopBest
set @return_top=@top_name
return @return_top
end

if @number=4
begin
select top (1) @top_name=[size_name] From View_Size_2_TopBest
set @return_top=@top_name
return @return_top
end

if @number=5
begin
select top (1) @top_name=[group_name] from View_ProductGroup_TopBest
set @return_top=@top_name
return @return_top
end




如何在C#应用程序中使用@return_top


请帮助我


谢谢很多




How Do I use From @return_top In C# Application


Please Help Me


Thank A Lot

推荐答案

SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter parm=new SqlParameter("@pkid",SqlDbType.Int);
parm.Value=1;
parm.Direction =ParameterDirection.Input ; 
cmd.Parameters.Add(parm); 
SqlParameter parm2=new SqlParameter("@ProductName",SqlDbType.VarChar); 
parm2.Size=50; 
parm2.Direction=ParameterDirection.Output; // This is important!
cmd.Parameters.Add(parm2); 
cn.Open(); 
cmd.ExecuteNonQuery();
cn.Close(); 

// Print the output value
Console.WriteLine(cmd.Parameters["@ProductName"].Value); 
Console.ReadLine();


由于@return_top是该过程的输出参数,因此应在过程内部设置它的值.您实际上并没有返回"它,只需设置值

调用过程时,定义参数,然后将参数设置为Directionoutput.请参见 SqlParameter.Direction属性 [ParameterDirection枚举 [
Since the @return_top is an output parameter for the procedure, you should set it''s value inside the procedure. You don''t actually ''return'' it, only set the value

When you call the procedure, you define the parameter, you set the parameter.Direction to output. See SqlParameter.Direction Property [^] and ParameterDirection Enumeration[^]


这篇关于如何将此nvarchar返回给C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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