我的存储过程是:
create Procedure spSetUser
(
@Name NVarchar(50),
@OrganicTitle NVarchar(30),
@UserName NVarchar(20),
@Password NVarchar(16),
@Result Int Output
)
As
Begin
Set @Result = -1
If Not Exists(Select UserId From dbo.Users Where UserName=@UserName)
Begin
Insert Into dbo.Users (Name,OrganicTitle,UserName,[Password])
Values(@Name,@OrganicTitle,@UserName,@Password)
Set @Result = SCOPE_IDENTITY()
End
Return
End
Go
我的课是(在微软企业库中):
[DataObjectMethod(DataObjectMethodType.Insert)]
public Int32 SetUser(UserFieldSet Data)
{
Int32 Result = 0;
object[] values = new object[] { Data.Name, Data.OrganicTitle, Data.UserName, Data.Password, Data.UserId };
Result = Db.ExecuteNonQuery("spSetUser", values);
return Result;
}
结果是执行存储过程的行影响,但我想获取@Result的值(过程的输出参数),我怎样才能得到它?
UserFieldClass 是:
public class UserFieldSet
{
public Int32 UserId;
public String Name;
public String OrganicTitle;
public String UserName;
public String Password;
}
提示:我知道必须使用此代码获取 @Result:
public Int32 SetUser(UserFieldSet Data)
{
Int32 Result = 0;
DbCommand DbCmd = Db.GetStoredProcCommand("spSetUser");
Db.AddInParameter(DbCmd, "@Name", DbType.String, Data.Name);
Db.AddInParameter(DbCmd, "@OrganicTitle", DbType.String, Data.OrganicTitle);
Db.AddInParameter(DbCmd, "@UserName", DbType.String, Data.UserName);
Db.AddInParameter(DbCmd, "@Password", DbType.String, Data.Password);
Db.AddOutParameter(DbCmd, "@Result", DbType.Int32, Int32.MaxValue);
Db.ExecuteNonQuery(DbCmd);
Result = (Int32)Db.GetParameterValue(DbCmd, "@Result");
return Result;
}
但我可以用这个方法得到它:
Result = Db.ExecuteNonQuery("spSetUser", values);