I am new to programming in ASP.NET and C #.
I would like to know what is the difference and advantages plus the disadvantages of using parameters instead of concatenation in SQL statements, since I heard that this is the best way to prevent SQL injection (?)
The following are examples of INSERT statements that I changed using parameters to concatenation:
Concatenation:
string sql = string.Format("INSERT INTO [UserData] (Username, Password, ...) VALUES ('" + usernameTB.Text + "', '" + pwTB.Text + "',...);
Options:
cmd.CommandText = "INSERT INTO [UserData] (Username, Password, ...) VALUES (@Username, @Password, ...)"; cmd.Parameters.AddWithValue("Username", usernameTB.Text); cmd.Parameters.AddWithValue("Password", pwTB.Text);
Thank you in advance for the knowledge provided.
source share