I am working with c# and access,
this is my code:
foreach (var item in students) { OleDbDataReader reader = Connection.Read("SELECT * FROM Student WHERE cod=" + item.cod); if (reader.Read()) { } else { MSAConnection.execute("INSERT INTO Student (cod, type Student, name, Paternal, Paternal, Full Name, degree, parallel, cel, CelT, codSchool) " + "values (\"" + Convert.ToInt32(item.code) + "\",\"" + studentType + "\",\"" + item.names.ToUpper() + "\",\"" + item.Parent.ToUpper() + "\",\"" + item.Parent.ToUpper() + "\",\"" + item.fullname.ToUpper() + "\",\"" + getGrade(item.grade) + "\",\"" + getParallel(item.parallel).ToUpper() + "\",\"" + Convert.ToInt32(getNumber(item.cel)) + "\",\"" + Convert.ToInt32(getTutorNumber(item.CelT)) + "\",\"" + Convert.ToInt32(item.school) + "\")"); } }
I don't know how you implement it
Connection.Read()
but it is clear that if it doesn't allow parameters you should stop using it.You could use something like:
You will see that the command is defined only once and then parameters are assigned during each iteration of the
foreach
In addition, a method is used to validate if there is
in this way the code is neat and maintainable, it is important to define the connection within the using
if you open a read() don't forget to close it.