My idea is to create some common classes for Insert / Update / Select through a C # (3.5) Winforms application that talks to a MySQL database through 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 the query with / without user input, simply passing the SQL query string.
Reading on SO is starting to give me an indication that this could lead to SQL injection attacks (for any custom input values). Is there a way to clear the entered strSQL, or do I need to go and create individual parameterized queries in each method that should execute the database function?
Update1:
My final solution looks something like this:
public void Insert(string strSQL,string[,] parameterValue) { if (this.OpenConnection() == true) { MySqlCommand cmd = new MySqlCommand(strSQL, connection); for(int i =0;i< (parameterValue.Length / 2);i++) { cmd.Parameters.AddWithValue(parameterValue[i,0],parameterValue[i,1]); } cmd.ExecuteNonQuery(); this.CloseConnection(); }}
John m
source share