如何获取存储过程中output的值

Caoxp_papa 2010-02-02 05:17:04

create proc getstr @str varchar(50) output
as
set @str = @str + 'aaa'


public static string getstr(string str)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand com = new SqlCommand("getstr", con))
{
string outputstr = "";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@str", str);
com.Parameters["@str"].Direction = ParameterDirection.InputOutput;
con.Open();
com.ExecuteNonQuery();
outputstr = com.Parameters["@str"].Value.ToString();
return outputstr;
}
}
}

我给@str传的参数为q,最后outputstr为q,怎么让他得到qaaa呢?
...全文
338 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
随风落梦 2010-02-03
  • 打赏
  • 举报
回复
myParameter.Direction = ParameterDirection.Output;
qawszx 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qawszx 的回复:]
C# code
com.Parameters.Add("@str", str);
com.Parameters["@str"].Direction= ParameterDirection.InputOutput;
con.Open();
com.ExecuteNonQuery();
outputstr= com.Parameters["@str"].Value.ToString();

C# code
com.Parameters.Add("@str",SqlDbType.NVarchar,10));
com.Parameters["@str"].Direction=ParameterDirection.Output;
con.Open();
com.ExecuteNonQuery();
outputstr= com.Parameters["@str"].Value.ToString();

[/Quote]


存储过程

create proc getstr @str varchar(50) output
as
set @str = 'aaa'
jenny0810 2010-02-03
  • 打赏
  • 举报
回复

public static string getstr(string str)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand com = new SqlCommand("getstr", con))
{
string outputstr = "";
com.CommandType = CommandType.StoredProcedure;
SqlParameter sp1 = new SqlParameter("@str",SqlDbType.VarChar,10);
sp1.Value = str;
com.Parameters.Add(sp1);

com.Parameters["@str"].Direction = ParameterDirection.InputOutput;
con.Open();
com.ExecuteNonQuery();
outputstr = com.Parameters["@str"].Value.ToString();
return outputstr;
}
}
}

这样试试
jenny0810 2010-02-03
  • 打赏
  • 举报
回复
标记
ilovey4 2010-02-03
  • 打赏
  • 举报
回复
友情帮顶
yanzhiyong 2010-02-03
  • 打赏
  • 举报
回复
上面是com了,带OUT或者return 的不要用ADD方法,和AddWithValue方法。这样会把他当成一般的参数的。

因为这两个方法是封装了的。。
yanzhiyong 2010-02-03
  • 打赏
  • 举报
回复
public static string getstr(string str)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand com = new SqlCommand("getstr", con))
{
string outputstr = "";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@str", str);
com.Parameters["@str"].Direction = ParameterDirection.InputOutput;
con.Open();
com.ExecuteNonQuery();
outputstr = com.Parameters["@str"].Value.ToString();
return outputstr;
}
}
}



com.Parameters.Add("@str", str);
com.Parameters.AddWithValue("@str", str);
这句话有错误,ADD方法已经过时,AddWithValue方法是最新的,不过这两种方法取得的应该只是你赋值的内容。。
ParameterDirection.InputOutput 这个是先传入再传出的。直接用Output是取不到值的。

正确的方法应该是这样


SqlParameter param= new SqlParameter("@str", SqlDbType.VarChar, 50);
param.Direction = ParameterDirection.InputOutput;
param.Value = str;
cmd.Parameters.Add(param);


RUNBEAR 2010-02-02
  • 打赏
  • 举报
回复
试下先close掉con,再取。
cailee 2010-02-02
  • 打赏
  • 举报
回复
@str是null,set @str =@str+ 'aaa'是null



create proc getstr @str varchar(50) output
as
set @str = 'aaa'


public static string getstr(string str)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand com = new SqlCommand("getstr", con))
{
string outputstr = "";
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("@str", str);
com.Parameters["@str"].Direction = ParameterDirection.Output;
con.Open();
com.ExecuteNonQuery();
outputstr = com.Parameters["@str"].Value.ToString();
return outputstr;
}
}
}


jack15850798154 2010-02-02
  • 打赏
  • 举报
回复
sql server2005返回值问题 收藏
(1)关于存储过程返回值问题(output,ruturn)
output存储过程:

alter proc usp_update
@count int output
as
set @count=(select count(*) from stu)



return存储过程:
alter proc usp_update
as
declare @count int
set @count=(select count(*) from stu)
return @count


.net代码(output):
SqlConnection con = new SqlConnection ("server=95F188CF1A24424;uid=jinzhiyuan;pwd=jinzhiyuan;database=student");
con.Open();
SqlCommand cmd = new SqlCommand("usp_update",con);
cmd.Parameters.Add("@count", SqlDbType.Int);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters["@count"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();

int j=(int)cmd.Parameters["@count"].Value;
Response.Write("我是刚刚才执行的值:"+j);
con.Close();


.net代码(return)
SqlConnection con = new SqlConnection("server=95F188CF1A24424;uid=jinzhiyuan;pwd=jinzhiyuan;database=student");
con.Open();
SqlCommand cmd = new SqlCommand("usp_update",con);
cmd.Parameters.Add("@count", SqlDbType.Int);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters["@count"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();

int j=(int)cmd.Parameters["@count"].Value;
Response.Write("我是刚刚才执行的值:"+j);
con.Close();


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jack15850798154/archive/2009/10/29/4742747.aspx
Caoxp_papa 2010-02-02
  • 打赏
  • 举报
回复
最上边有啊
Rock870210 2010-02-02
  • 打赏
  • 举报
回复
把你存储过程贴出来
Caoxp_papa 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qawszx 的回复:]
C# code
com.Parameters.Add("@str", str);
com.Parameters["@str"].Direction= ParameterDirection.InputOutput;
con.Open();
com.ExecuteNonQuery();
outputstr= com.Parameters["@str"].Value.ToString();

C# code
com.Parameters.Add("@str",SqlDbType.NVarchar,10));
com.Parameters["@str"].Direction=ParameterDirection.Output;
com.ExecuteNonQuery();
outputstr= com.Parameters["@str"].Value.ToString();

[/Quote]
这样outputstr成“”了
Caoxp_papa 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rock870210 的回复:]
SqlParameter  myParameter  =  new  SqlParameter("@str",  SqlDbType.VarChar); 
  myParameter.Direction  =  ParameterDirection.Output; 
执行com.ExecuteNonQuery();完成后,myParameter  的值就是你要的output的值
[/Quote]
还是q啊
qawszx 2010-02-02
  • 打赏
  • 举报
回复


com.Parameters.Add("@str", str);
com.Parameters["@str"].Direction = ParameterDirection.InputOutput;
con.Open();
com.ExecuteNonQuery();
outputstr = com.Parameters["@str"].Value.ToString();



com.Parameters.Add("@str",SqlDbType.NVarchar,10));
com.Parameters["@str"].Direction=ParameterDirection.Output;
com.ExecuteNonQuery();
outputstr = com.Parameters["@str"].Value.ToString();

Rock870210 2010-02-02
  • 打赏
  • 举报
回复
SqlParameter myParameter = new SqlParameter("@str", SqlDbType.VarChar);
myParameter.Direction = ParameterDirection.Output;
执行com.ExecuteNonQuery();完成后,myParameter 的值就是你要的output的值
Caoxp_papa 2010-02-02
  • 打赏
  • 举报
回复
在存储过程能直接取出来,但是不知道怎么在程序里取
Rock870210 2010-02-02
  • 打赏
  • 举报
回复
你在存储过程中output qaaa,取到的就是qaaa
Caoxp_papa 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 rock870210 的回复:]
outputstr = com.Parameters["@str"].Value.ToString();
这样已经取出来了
[/Quote]
可我取出来的是q啊
Rock870210 2010-02-02
  • 打赏
  • 举报
回复
outputstr = com.Parameters["@str"].Value.ToString();
这样已经取出来了

62,265

社区成员

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

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

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

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