存储过程,传入一个参数,返回一个参数

coffeedou 2004-11-30 03:18:55
请帮我写一个完整的C#。NET的例子,调用这个存储过程,并得到返回值。
...全文
149 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
coffeedou 2004-11-30
  • 打赏
  • 举报
回复
存储过程:
CREATE PROCEDURE p_getdepartcode @parent_code varchar(10),@depart_code_out varchar(10) output AS
begin
declare @departcode varchar(10)
set @departcode = (select MAX(depart_code) from bas_depart where len(depart_code)-2=len(@parent_code) and

left(depart_code,len(parent_code)) = @parent_code )
if @departcode is null
set @departcode = @parent_code + '01'
else
set @departcode = str(@departcode + 1)
set @depart_code_out = @departcode
return @depart_code_out
end
GO

C#,ASP.NET中:
try
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connstring"]);
SqlCommand cmd = new SqlCommand("p_getdepartcode",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] Parameters = new SqlParameter[2];
Parameters[0] = new SqlParameter("@parent_code",Request.QueryString["departcode"]);
Parameters[1] = new SqlParameter("@depart_code_out",SqlDbType.VarChar,10);
Parameters[1].Direction = ParameterDirection.Output;
cmd.Parameters.Add(Parameters);
cmd.ExecuteNonQuery();
depart_code.Text = Parameters[1].Value.ToString();
}
catch
{
Response.Write("执行存储过程错误,请确认您输入的参数!");
}

哪儿错了?
YUAN168 2004-11-30
  • 打赏
  • 举报
回复
CREATE function p_getdepartcode @parent_code varchar(10)
RETURNS VARCHAR(10)
begin
declare @departcode varchar(10)
set @departcode = (select MAX(depart_code) from bas_depart where len(depart_code)- 2=len(@parent_code) and

left(depart_code,len(parent_code)) = @parent_code )
if @departcode is null
set @departcode = @parent_code + '01'
else
set @departcode = str(@departcode + 1)
RETURN @departcode
end
coffeedou 2004-11-30
  • 打赏
  • 举报
回复
select 不能返回值吗?
memgarden 2004-11-30
  • 打赏
  • 举报
回复
这个存储过程不带输出参数和返回值呀
sweet12345 2004-11-30
  • 打赏
  • 举报
回复
同样的,输入的参数,可以不用指定direction,默认的就是输入参数(input)
sweet12345 2004-11-30
  • 打赏
  • 举报
回复
dim par as new sqlparameter
par.direction=output
.....
sqlcommand.sqlparametter.add(par)
定义了par的direction后,调用了sqlcommand.execute后,就可以将返回值自动赋给par了。
brightheroes 2004-11-30
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3601/3601723.xml?temp=.1158716
brightheroes 2004-11-30
  • 打赏
  • 举报
回复
你这个存储过程并没有返回值
coffeedou 2004-11-30
  • 打赏
  • 举报
回复
CREATE PROCEDURE p_getdepartcode @parent_code varchar(10 AS
begin
declare @departcode varchar(10)
set @departcode = (select MAX(depart_code) from bas_depart where len(depart_code)-2=len(@parent_code) and

left(depart_code,len(parent_code)) = @parent_code )
if @departcode is null
set @departcode = @parent_code + '01'
else
set @departcode = str(@departcode + 1)
select @departcode
end
GO
yaterman2 2004-11-30
  • 打赏
  • 举报
回复
存储过程在那里阿

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧