C# and MySQL .NET Connector - Any way of preventing SQL Injection attacks in a generic class?

Posted by John M on Stack Overflow See other posts from Stack Overflow or by John M
Published on 2010-05-05T18:13:14Z Indexed on 2010/05/05 18:18 UTC
Read the original article Hit count: 382

Filed under:
|
|

My idea is to create some generic classes for Insert/Update/Select via a C# (3.5) Winforms app talking with a MySQL database via MySQL .NET Connector 6.2.2.

For example:

public void Insert(string strSQL)
{
   if (this.OpenConnection() == true)
   {
       MySqlCommand cmd = new MySqlCommand(strSQL, connection);
       cmd.ExecuteNonQuery();
       this.CloseConnection();
   }
}

Then from anywhere in the program I can run a query with/without user input by just passing a SQL query string.

Reading around on SO is starting to give me the indication that this may lead to SQL injection attacks (for any user-input values). Is there anyway of scrubbing the inputted strSQL or do I need to go and create individual parameterized queries in every method that needs to do a database function?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about .NET