Generally speaking, yes*.
Using proper escaping of input is a recommendation from OWASP as a valid approach to avoiding SQL Injection and while it may handle all of your needs, it's worth listing their preferences to avoid SQL Injection attacks :
- Option 1: Using Parameterized Queries
- Option 2: Using Stored Procedures
- Option 3: Escaping All User Supplied Input (Your Approach)
So as you can see, properly escaping the input falls third on the list, but for all intents and purposes, it should be enough to avoid SQL Injection. I'm not a big advocate of the approach personally, but that's just my opinion.
Recommendation: Use Parameterization When You Can
While using quotes can help, you should consider using proper parameterization to take advantage of some of the built-in protection that the .NET framework provides to check that the parameters are of the proper type, etc. if it is an option :
// Define a parameter in your query using the @parameter format (or ? in OleDbConnections)
var sql ="SELECT * FROM Users WHERE Username = @username";
using(var command = new SqlCommand(connection, sql))
{
// Ensure your connection is open and other code here...
// Add your parameter
command.Parameters.AddWithValue("@username",txtUsername.Text);
// Any other logic here...
// Execute your query
using(var reader = command.ExecuteReader())
{
// Do your thing...
}
}
When Parameterization Is Not An Option
As TomTom mentions, there are some scenarios when parameterization might be possible or practical due to implementation (i.e. too many parameters required) or performance limitations. In these scenarios, OWASP recommends the use of stored procedures or your current technique of escaping input, which implies you can rely on using quotes, but just be careful and consider sanitizing your input as much as possible.