我有一个数据库表,我现在在其中保存创建的用户和用户名,如果用户名存在于数据库中,我想从我的数据库中获取字符串值,然后我将显示错误消息“用户名存在或选择新的等”
这是我的存储过程
ALTER PROCEDURE [dbo].[p_SaveUpdate_AdminUserAccount]
(
@Id_User int,
@nm_UserName varchar(50),
@nm_UserPassword varchar(50),
@nm_UserRole int
)
AS
BEGIN
IF @Id_User=0
BEGIN
Declare @count int
Select @count=COUNT(1) from ref_Admin_UserAccount where nm_UserName=@nm_UserName
if(@count > 0)
begin
select -1 as a;
end
else
BEGIN
SET NOCOUNT ON;
Insert into ref_Admin_UserAccount(nm_UserName,nm_UserPassword,nm_UserRole)
values(@nm_UserName,@nm_UserPassword,@nm_UserRole)
SELECT SCOPE_IDENTITY();
end
END
ELSE
BEGIN
SET NOCOUNT ON;
UPDATE ref_Admin_UserAccount
SET
nm_UserName=@nm_UserName,
nm_UserPassword=@nm_UserPassword,
nm_UserRole=@nm_UserRole
WHERE Id_User=@Id_User;
SELECT @Id_User;
End
End
这是我使用 3 层架构从表示层将值保存在数据库中的代码
public int SaveAdminUserAccountInformation(AdminAccountProperties oAdminUser)
{
try
{
SqlParameter[] parm = new SqlParameter[4];
parm[0] = new SqlParameter(PARM_ADMIN_USER_ID, SqlDbType.Int);
parm[0].Value = oAdminUser.UserID;
parm[1] = new SqlParameter(PARM_USER_NAME, SqlDbType.VarChar);
parm[1].Value = oAdminUser.UserName;
parm[2] = new SqlParameter(PARM_ADMIN_USER_PASSWORD, SqlDbType.VarChar);
parm[2].Value = oAdminUser.Password;
parm[3] = new SqlParameter(PARM_USER_ROLE, SqlDbType.Int);
parm[3].Value = oAdminUser.UserRole;
return Convert.ToInt32(SqlHelper.ExecuteScalar(this._ConnString, CommandType.StoredProcedure, SQL_ADMIN_USER_INSERT_UPDATE, parm));
}
catch (Exception ex)
{
throw ex;
}
}
请给我一些帮助。