SQL Injection is when people try to hack the database to get data that they should not have access to, or deliberately try to sabotage it.
The way it works is this....
Suppose you have a login box on a website. People are expected to type in their user name and password. In the code, you have something like this...
[tt]SQL = "Select * From Users Where UserName = '" & txtUserName.Text & "' and Password = '" & txtPassword.Text & "'"[/tt]
You then open a recordset and check to see how many rows are returned. 0 rows means the user name or password is incorrect. 1 row means the username and password are valid. Simple, right?
Well.... suppose the "Bad hacker guy" uses values like this (without the quotes):
username = "Blah"
password = "'; Drop Table Users;"
When you build your SQL string, you end up with:
[tt]SQL = "Select * From Users Where UserName = 'Blah' and Password = ''; Drop Table Users;'[/tt]
Or:
[tt]Select * From Users Where UserName = 'Blah' and Password = ''; [!]Drop Table Users;[/!]'[/tt]
The first select part wouldn't return any rows, but the DROP TABLE USERS would still be executed. Suddenly you have lost your users table and nobody can log in to your application any more.
Alternatively, the "Bad Hacker Guy" could use these values:
Username = "Blah"
Password = "' or 1=1;"
You SQL would look like this:
[tt]SQL = "Select * From Users Where UserName = 'Blah' and Password = '' or 1=1;'[/tt]
The "or 1=1" part would cause the query to return all the rows. Since there are rows, your app may interpret this as a successful login and now this guy has access to your application and all your data.
The simplest way to protect yourself from SQL Injection is to use the command object with parameters. Given the above scenarios, the attempt by the hacker to destroy your data and/or gain access to your data would not happen because the command object doesn't just build a SQL String. Behind the scenes it is parameterizing the query and the entire SQL Inject hacky stuff would be passed in for the password. It wouldn't match so the user would not gain access to your data. The DROP TABLE part would be seen as the password, not as additional SQL code to run.
Does this make sense?
I encourage you to read more about this: [google]SQL Injection[/google]
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom