求存储过程的返回值。

youyuan1980 2006-03-08 04:56:06
我是想取得自动增量的值。那个full有没有没什么用。
存储过程如下。
表为dingdan_info
id bigint
full nvarchar

CREATE PROCEDURE add_ding(@id bigint output) AS
insert into dingdan_info([full]) values(1) select @@identity as id
return
GO
代码如下

public System.Int64 add_ding()
{System.Data .SqlClient .SqlCommand xx=this.mycommand ("add_ding");
xx.Parameters .Add (new SqlParameter ("@id",System.Data .SqlDbType .BigInt ));
xx.Connection .Open ();
xx.ExecuteNonQuery ();
System.Int64 id=System.Convert .ToInt64 (xx.Parameters["@id"].Value .ToString ());
xx.Connection .Close ();
return id;}

执行以后。说存储过程需要@id参数。我的这个@id是输出的。。。
求大侠帮忙解答一下
...全文
227 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
amnoh 2006-03-09
  • 打赏
  • 举报
回复
晕,结贴倒是很快
amnoh 2006-03-09
  • 打赏
  • 举报
回复
问题就在这里了:
yy.Direction =System.Data .ParameterDirection .Output ;
System.Int64 id=System.Convert .ToInt64 (xx.Parameters ["@id"].Value );
xx.Connection .Open ();
xx.ExecuteNonQuery ();
xx.Connection .Close ();
return id;}


你注意看看,你的id取值发生在什么时候,是在执行查询前的呀,那时候当然是0,应当把
System.Int64 id=System.Convert .ToInt64 (xx.Parameters ["@id"].Value );
放在
xx.Connection .Close ();
后边才对
bbbbcccc 2006-03-09
  • 打赏
  • 举报
回复
http://valenhua.go3.icpcn.com/
todouwang 2006-03-09
  • 打赏
  • 举报
回复
direction=pdoutput;
dfkjewyoldfjkleoe 2006-03-09
  • 打赏
  • 举报
回复
www.source520.com 免费免注册80G源码书籍下载
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
存储过程我也在里面查询分析器中算过了,没问题,
应该还是代码中取值的问题。
请大家继续帮忙啊
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
修改过的。
代码如下
public System.Int64 add_ding()
{System.Data .SqlClient .SqlCommand xx=this.mycommand ("add_ding");
System.Data .SqlClient .SqlParameter yy;
yy=xx.Parameters .Add ("@id",System.Data .SqlDbType .BigInt );
yy.Direction =System.Data .ParameterDirection .Output ;
System.Int64 id=System.Convert .ToInt64 (xx.Parameters ["@id"].Value );
xx.Connection .Open ();
xx.ExecuteNonQuery ();
xx.Connection .Close ();
return id;}

存储过程如下
CREATE PROCEDURE add_ding(@id bigint output) AS
insert into dingdan_info([full]) values(1)
select @id=@@identity
return
GO

为何取的值始终为零,不得其解
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
一个很奇怪的问题,为什么值总是为零呢。。。
不是说自动添加的嘛。
daishengs 2006-03-08
  • 打赏
  • 举报
回复
string strSQL ="Insert into JXC_Input (Code) VALUES ('"+InPutCode+"');select @@IDENTITY;";



youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
minajo21(大眼睛) ( ) 信誉:100
大眼睛的方法不对。。
得出来的只是返回影响的行数,始终是1
daishengs 2006-03-08
  • 打赏
  • 举报
回复
myReader = Database.ExecuteReader(Database.connString,CommandType.Text,"Insert into JXC_Input (Code) VALUES ('"+InPutCode+"');select @@IDENTITY;");
while(myReader.Read())
{
ID=Convert.ToString(myReader[0]);
}
goody9807 2006-03-08
  • 打赏
  • 举报
回复
http://goody9807.cnblogs.com/archive/2005/07/08/188475.html
里面有一段代码你可以参考!
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
不对吧,我取的为什么始终都是零啊。
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
xx.Parameters .Add ("@id",System.Data .SqlDbType .BigInt ).Direction =
我是输出的,我也不知道应该等于多少。
我想就应该是
id=........吧。
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
楼上的,可以写全一点吧。。还是看得不太清楚。。。
谢谢。我这人比较笨,哈哈。
amnoh 2006-03-08
  • 打赏
  • 举报
回复
public System.Int64 add_ding()
{System.Data .SqlClient .SqlCommand xx=this.mycommand ("add_ding");
xx.Parameters .Add (new SqlParameter ("@id",System.Data .SqlDbType .BigInt ));
xx.Connection .Open ();
xx.ExecuteNonQuery ();
System.Int64 id=System.Convert .ToInt64 (xx.Parameters["@id"].Value .ToString ());
xx.Connection .Close ();
return id;}

====================================================
应当指定参数的Direction:
xx.Parameters .Add ("@id",System.Data .SqlDbType .BigInt ).Direction = ParameterDirection.Output;

SqlParameter默认不是Output的,所以会出错
youyuan1980 2006-03-08
  • 打赏
  • 举报
回复
好的,我看一下哦。。。。谢谢楼上的诸位。我去看一下。。好了就来结分。
minajo21 2006-03-08
  • 打赏
  • 举报
回复
不用写存储过程,这样写就可以了:

string strSQL = "insert into .... ";
strSQL += " ; SELECT new=@@IDENTITY";

// ....
int id = (int)objSqlCommand.ExecuteScalar();
shinji329 2006-03-08
  • 打赏
  • 举报
回复
CREATE PROCEDURE add_ding
(@id bigint output)
AS
insert into dingdan_info([full]) values(1)
select @id = @@identity
return
GO
shinji329 2006-03-08
  • 打赏
  • 举报
回复
CREATE PROCEDURE add_ding
(@id bigint output)
AS
insert into dingdan_info([full]) values(1)
select (@id = @@identity
return
GO

62,046

社区成员

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

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

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

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