I'm just starting out to learn all about sql databases. For now based on previous suggestions that was given to me online, I've incorporated a using statement to avoid problems in sql connection and then I applied parameterized queries to avoid sql injection. I was also been given an advice to use sqlparameter arrays to supplement the parameterized queries that i implemented. I just want to know if there are other techniques that you would recommend to improve my code, which i will indicate below. Feel free to suggest better solutions.
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["brandsConnection"].ToString())) {
string query = "UPDATE [guitarBrands] SET type = @type, name = @name, image = @image WHERE id = @id";
SqlParameter[] p = new SqlParameter[4];
p[0] = new SqlParameter("@type", newType.Text);
p[1] = new SqlParameter("@name", newName.Text);
p[2] = new SqlParameter("@image", newImage.Text);
p[3] = new SqlParameter("@id", id);
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
GetExample(command, p);
command.ExecuteNonQuery();
connection.Close();
command.Parameters.Clear();
}
}
public void GetExample(SqlCommand command, params SqlParameter[] p)
{
if (p != null && p.Any()){
command.Parameters.AddRange(p);
}
}