I have an ASP.Net MVC application which use SQL 2012 as the database server. I have used Views,Stored Procedures (With/Without dynamic sql queries). I 've heard that dynamic sql can be a victim of sql injection.
Here is one of my sample dynamic query..
DECLARE @Username AS Varchar(100);
DECLARE @Password AS Varchar(100);
SET @Username = 'user1';
SET @Password = '123';
DECLARE @Query AS VARCHAR(MAX);
SET @Query = 'SELECT * FROM USERS WHERE Username ='+ @Username+ ' AND Password = '+@Password+';
EXEC(@Query)
How can I write this query preventing sql injection?