Click here to Skip to main content
15,886,006 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
I have created the login table in sqlserver in that i have added user name and password.for this how to write stored procedure.

Regards
Balamurugan
Posted
Comments
Abhijit Parab 26-Sep-12 5:26am    
Hi,
Try something from your end. If dont know how to write store procedure then google it or do RnD on it. If your getting problem then post code here.
[no name] 26-Sep-12 5:30am    
Create proc SP_peerfilesharing
(
@L_Username nvarchar(50)
@L_Password nvarchar(50)
)
as
begin
Insert into Login(L_Username,L_Password) values (@L_Username,@L_Password)
end



I hav written the above nd i'm getting error so only posted here...

SQL
CREATE TABLE Usertable(
	[Pk_ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[UserID] [varchar](10) NOT NULL,
	[UserName] [varchar](50) NOT NULL,
	[Password] [binary](16) NOT NULL,
	[IncorrectLoginAttempts] [smallint] NOT NULL,--incorrect login attempts
	[LastIncorrectLoginAttemptsDate] [datetime] NOT NULL,-- last incorrect login attempt ate
	[LoginLockStat] [char](1) NOT NULL,-- user login locked or not
	[StatFlag] [char](1) NOT NULL,	-- to indicate user live or not
	[CrtdDt] [datetime] NOT NULL,
	[CrtdBy] [varchar](10) NOT NULL,
	[LstModDt] [datetime] NOT NULL,
	[LastPasswordChangeDate] [datetime] NOT NULL,
	[LstModBy] [varchar](10) NOT NULL,)

--Add primary to [UserID] column

--insert data into table

--login button click call store procedure here password store in the table in binary formate which is actually encrypted text.while checking for psw match send encrypted password to your login store procedure.

-- your login store procedure

if OBJECT_ID('SPLogin') is not null
	drop procedure SPLogin
Go 
    
CREATE procedure SPLogin    
(    
	
	@UserId					varchar(10),    
	@Password					binary(16),    
	@SocietyName			varchar(10),    
	@AccPeriodDesc			varchar(10)    
)    
as    
Begin    
 Declare @LoginLockStat as  char  
 Declare @InvalLoginAttmpt as int 
 
 set @InvalLoginAttmpt=(select IncorrectLoginAttempts from Usertable where UserID=@UserId) 
  
  -- validate user access
  
  if ((select LoginLockStat from Usertable where UserID=@UserId)='Y') 
  Begin
	  Raiserror('User access is Locked',16,1) 
	  Return -1 
  End
  
  if @InvalLoginAttmpt =3
  Begin
	  begin tran
			update Usertable 
			set IncorrectLoginAttempts =@InvalLoginAttmpt,
			LoginLockStat ='Y',
			LastIncorrectLoginAttemptsDate =GETDATE()
			where UserID=@UserId 
	  
	  If @@RowCount=0 OR @@Error <>0 
			Begin
				RaisError('Error while Updating data',16,1)
				rollback 
				Return -1 
			End 
	  commit
	  Raiserror('User access is Locked',16,1) 
	  Return -1 
  End
  
  
  -- validate Password expire

  -- to check whether password expire or not.this is optional whether you want force to user change password after every month
  
  if DATEDIFF (dd,convert(date,(select LastPasswordChangeDate  from Usertable s  where s.UserId like Upper(@UserId)),103),Getdate() )>=30
  Begin  
	Raiserror('Password Expired',16,1)
	Return -1
  End

  select * from SA_User_h
  -- =======================================================================================
	-- Query to Validate User
	-- =======================================================================================
  
	
	
		
		if exists(select null from Usertable 
				  where UserID=@UserId 
				  And Password=@Password
				  And StatFlag='L')
		Begin
			begin tran
			update Usertable 
			set IncorrectLoginAttempts =0
			where UserID=@UserId  
			
			If @@RowCount=0 OR @@Error <>0 
			Begin
				RaisError('Error while Updating data',16,1)
				rollback 
				Return -1 
			End 
			Commit
			select 'success ful login'
		End
		Else
		Begin  
			begin tran
			update Usertable 
			set IncorrectLoginAttempts =@InvalLoginAttmpt+1,
			LoginLockStat =(case when @InvalLoginAttmpt+1=3 then 'Y' else 'N' End ),
			LastIncorrectLoginAttemptsDate =GETDATE()
			where UserID=@UserId  
			
			If @@RowCount=0 OR @@Error <>0 
			Begin
				RaisError('Error while Updating data',16,1)
				rollback 
				Return -1 
			End 
			Commit
			
			if ((select LoginLockStat from Usertable where UserID=@UserId)='Y')
			Begin
				Raiserror('User access is Locked',16,1) 
				Return -1 
			End
			Else
			Begin
				Raiserror('Invalid Login or Password. Try Again',16,1)  
				Return -1  
			End	
		End  				  

End
 
Share this answer
 
v2
Comments
[no name] 26-Sep-12 6:23am    
Thanks for your solution...i got it right.....
SQL
Create proc SP_peerfilesharing 
@L_Username nvarchar(50), 
@L_Password nvarchar(50)  
as 
begin 
Insert into Login values(@L_Username,@L_Password) 
end 

copy paste this it will work
 
Share this answer
 
v2
Comments
[no name] 26-Sep-12 6:24am    
I got the solution....tks for ur code....
[no name] 26-Sep-12 6:43am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900