读书人

取存储过程输出参数值的有关问题(着急

发布时间: 2011-12-19 23:23:36 作者: rapoo

取存储过程输出参数值的问题(着急啊!!!!)
存储过程为:
ALTER PROCEDURE acticle_add

(
@title varchar(50),
@content varchar(500),
@PostId bigint output
)

AS
SET NOCOUNT ON
insert into acticle_msup(title,content) values(@title,@content)
select @PostId=@@IDENTITY

SET NOCOUNT OFF
RETURN
========================
方法为:
public void acticle_add(string title, string content,out int postid)
{

SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter( "@title ",SqlDbType.VarChar,50);
param[0].Value = title;
param[1] = new SqlParameter( "@content ",SqlDbType.VarChar,500);
param[1].Value = content;
param[2] = new SqlParameter( "@PostId ",SqlDbType.BigInt,8);
param[2].Direction = ParameterDirection.Output;

Helper.SqlHelper.ExecuteNonQuery(ConnString, "acticle_add ",param);
postid = Convert.ToInt32(param[2].Value);


}
====================
调用:
protected void acticle_add()
{

DAL.method method = new method();
int postid;
method.acticle_add(TextBox1.Text.ToString(),TextBox2.Text.ToString(),out postid);
Response.Write(postid);

这是我的程序,我的存储过程在查询分析器里运行是正确的,能返回标志列的ID,但是我在调用的时候,返回值都是0啊,我调试跟踪的时候 postid为 null ,请帮忙解答一下,谢谢了

[解决办法]
select @PostId=@@IDENTITY
->
set @PostId = @@IDENTITY
[解决办法]
set @PostId=@@IDENTITY
[解决办法]
首先 建议最好不要用@@IDENTITY 感觉SCOPE_IDENTITY( 'youTableName ')是最保险的
//////////////////////////////////////////////////////////////////////////////
LZ为什么不select @@IDENTITY
或者直接 select IDENT_CURRENT( 'acticle_msup ')
还要那个output参数干嘛
------解决方案--------------------


使用@IDENTITY
http://blog.csdn.net/octverve/archive/2007/09/04/1772462.aspx
[解决办法]
public void acticle_add(string title, string content,out int postid)
{

SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter( "@title ",SqlDbType.VarChar,50);
param[0].Value = title;
param[1] = new SqlParameter( "@content ",SqlDbType.VarChar,500);
param[1].Value = content;
//param[2] = new SqlParameter( "@PostId ",SqlDbType.BigInt,8);
//param[2].Direction = ParameterDirection.Output;
int nId; //返回的Id
nId=Helper.SqlHelper.ExecuteNonQuery(ConnString, "acticle_add ",param);
//postid = Convert.ToInt32(param[2].Value);
}

总感觉存储过程的output 有点鸡肋 直接select出来不好么 -,.- 费那事干嘛 是不是啊 道哥

读书人网 >asp.net

热点推荐