I want to use a variable and pass this value into an SQL statement as part of the table name, however, I'm not sure whether the method I've used is secure (even though it works). If not, how should I go about it?
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
string table = "1";
MySqlCommand comm = new MySqlCommand("INSERT INTO " + table + "_tests (user_id, score, time) VALUES (@user_id, @score, @time)", conn);
comm.Parameters.Add("@user_id", MySqlDbType.VarChar);
comm.Parameters["@user_id"].Value = "2";
comm.Parameters.Add("@score", MySqlDbType.VarChar);
comm.Parameters["@score"].Value = txtScore.ToString();
comm.Parameters.Add("@time", MySqlDbType.VarChar);
comm.Parameters["@time"].Value = txtTime.ToString();
Updated:
int tableID = Convert.ToInt32(Session["TestModuleID"].ToString());
MySqlCommand comm = new MySqlCommand("INSERT INTO " + tableID.ToString() + "_tests (user_id, score, time) VALUES (@user_id, @score, @time)", conn);
int.