利用存储过程得到新增数据的ID
数据库中有自增字段UID
存储过程如下:
CREATE PROCEDURE AddUser
(
@Username nvarchar(50),
@Email nvarchar(100),
@Password nvarchar(50),
@UserID int OUTPUT
)
AS
INSERT INTO Users
(
Username,
Email,
Password
)
VALUES
(
@Username,
@Email,
@Password
)
SELECT
@UserID = @@Identity
GO
程序如下:
SqlCommand cmd=new SqlCommand( "AddUser ",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter( "@Username ",Username));
cmd.Parameters.Add(new SqlParameter( "@Email ",Email));
cmd.Parameters.Add(new SqlParameter( "@Password ",Password));
然后如何得到刚刚插入记录的UID的值呢?
在这里我写不下去了,望大哥们指教.......
------解决方案--------------------indentdy UserID
CREATE PROCEDURE AddUser
(
@UserID int OUTPUT
@Username nvarchar(50),
@Email nvarchar(100),
@Password nvarchar(50),
)
------解决方案--------------------indentdy UserID
CREATE PROCEDURE AddUser
(
@UserID int ,
@Username nvarchar(50),
@Email nvarchar(100),
@Password nvarchar(50),
)
------解决方案--------------------SqlCommand cmd=new SqlCommand( "AddUser ",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter( "@Username ",Username));
cmd.Parameters.Add(new SqlParameter( "@Email ",Email));
cmd.Parameters.Add(new SqlParameter( "@Password ",Password));
cmd.parameters.add(new sqlparameter( "@UserID ",sqltype.int);
cmd.parameters[ "@userid "].direction = Direction.output;
CMD执行
int UID;
UID = cmd.Parameters[ "@userid "];
------解决方案--------------------