储存过程的问题
// 设置参数
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()
可以保证