读书人

储存过程的有关问题

发布时间: 2012-01-31 21:28:41 作者: rapoo

储存过程的问题
// 设置参数
SqlParameter param;
param = command.Parameters.Add( "@id ", SqlDbType.Int);
param.Direction = ParameterDirection.Output;
param = command.Parameters.AddWithValue( "@name ", idName.Text);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
param = command.Parameters.Add( "@num ", idNum.Text);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.Int16;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "spAddOrder ";

command.ExecuteNonQuery();

// 获取得到的id
string id = command.Parameters[ "@id "].Value.ToString();
Label1.Text = "( " + id + ") ";


插入是成功了
但取不到id值

储存过程
CREATE PROCEDURE spAddOrder
@name nchar,
@num integer,
@id integer out
as
Insert into [cloud] ( name, num) values(@name, @num);

GO


[解决办法]
begin tran:Insert into [cloud] ( name, num) values(@name, @num);select @@identity as id;commit;
[解决办法]
CREATE PROCEDURE spAddOrder
@name nchar,
@num integer,
@id integer out
as
Insert into [cloud] ( name, num) values(@name, @num);
select @id = @@indentity
GO
[解决办法]
select @id = SCOPE_INDENTITY()
可以保证

读书人网 >asp.net

热点推荐