I'm trying to create a re-usable MySQL database class in c# but I'm concerned about keeping it secure (Possible business use). I've done a lot of reading and people recommend using parameters to add data into the table and fetch it etc. My problem is keeping the class re-usable through a number of different tables My Insert Method Currently (Inside a database class):
public void Insert(string incomingQuery) //Insert Statement
{
//Assign the incomingQuery for use.
string query = incomingQuery;
//Open Database Connection
if(this.OpenConnection() == true)
{
//Create command and assign the query and connection from the constructor
MySqlCommand command = new MySqlCommand(query, connection);
//Execute the command
command.ExecuteNonQuery();
//Close the connection
this.CloseConnection();
}
}
And my Create
method passing the SQL Query in from another class (Users
):
public void Create()
{
//Add a new user to the database
string sqlQuery = "INSERT INTO Users(first_name, last_name, pc_username) VALUES('" + firstName + "','" + lastName + "','" + userName + "');";
database.Insert(sqlQuery);
Login();
}
How can I make this more secure?