I have the below method that saves data into a SQL Server database
public static int ExecuteNonQuery(string cmdText)
{
if(con.State == ConnectionState.Closed)
con.Open();
SqlCommand SC = new SqlCommand(cmdText, con);
return SC.ExecuteNonQuery();
}
My issue is: this is prone to SQL injection, so the best way out is to use parameters and also if I want to save special characters like single quote, that is not possible so the parameter option to me is the best.
My question now is how do I handle that when am using a method since I can't determine the number of parameters a SQL statement would need
This is how I call it
return Database.ExecuteNonQuery("INSERT INTO UPR00112(EMPLOYID, UNIVERSITY, DEGREE, MAJOR, GPA, GPABASE, GRADUATIONYEAR, SUPID, NOTE, ATTACHMENT, USERDEF1, STATUS, TYP) VALUES('" + EmpID + "','" + Uni + "','" + Degree + "','" + Major + "','" + GPA + "','" + GPABase + "','" + GYear + "'," + SupID + ",'" + Note + "','" + Attach + "','" + UserName + "','" + Status + "','" + Typ + "')");
SqlCommand.ExecuteNonQuery. So by itself this function adds not very much. Which is a good thing in your case. You should check hocmdTextis constructed, and where this function is used. That way you'll know the impact of any changes.DataBase.InsertUpr(employId, university, degree... etc);See for example: stackoverflow.com/questions/293311/…