3
I am building a program to insert data from excel files into a database.
My database has this structure:
Tables (and fields):
- Schedule (Id (PK), Starttime, Endtime, Dayweek, Roomid (FK), Classid (FK), Subjectid (FK), Teacherid (FK))
- Rooms (Roomid (PK), Roomname)
- Classes (Classid (PK), Classname)
- Subjects (Subjectid (PK), Subjectname)
- Teachers (Teacherid (PK), Teachername)
The methods are similar to this:
context.Horarios.Add(
new Horarios
{
Cod_Tempo = cod_Tempo,
Dia_Semana = dia_Semana,
Cod_Disciplina = ObterCodDisciplina(disciplina),
Cod_Professor = ObterCodProfessor(nomeProfessor),
Cod_Sala = ObterCodSala(sala),
Cod_Turma = ObterCodTurma(turma)
});
private int? ObterCodDisciplina(string disciplina)
{
using (var context = new ScheduleDatabaseEntities())
{
var dis = context.Disciplinas.FirstOrDefault(a => a.Disciplina == disciplina);
if (dis == null)
{
var disciplinaEntity = new Disciplinas {Disciplina = disciplina};
context.Disciplinas.Add(disciplinaEntity);
context.SaveChanges();
return disciplinaEntity.Cod_Disciplina;
}
else
return dis.Cod_Disciplina;
}
}
The purpose of code is to insert it into the relational table if there is no field with that name already and associate it with Row to be filled in. This way it works, but I wanted a way to improve the performance, since this is a little slow to execute the query, some suggestion?
Edit:
Complete Code, to understand the objective of the program:
private void btn_addtodb_Click(object sender, EventArgs e)
{
try
{
stopwatch.Start();
using (var context = new ScheduleDatabaseEntities())
{
foreach (string fullfilePath in ExcelfilesPath)
{
Excel.Workbook theWorkbook = app.Workbooks.Open(fullfilePath);
for (int i = 1; i <= theWorkbook.Worksheets.Count; i++)
{
Excel.Worksheet theWorksheet = theWorkbook.Worksheets[i];
Excel.Range excelRange = theWorksheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
string nomeProfessor = Convert.ToString(valueArray[9, 2]);
for (int k = 4; k <= 12; k = k + 2)
{
for (int j = 16; j <= 35; j = j + 2)
{
if (j == 24)
j--;
if (valueArray[j, k] != null)
{
int cod_Tempo = Convert.ToInt32(valueArray[j, 1]);
string hora_Inicial = Convert.ToString(valueArray[j, 2]);
string hora_Final = Convert.ToString(valueArray[j, 3]);
string sala = Convert.ToString(valueArray[j, k + 1]);
string dia_Semana = Convert.ToString(valueArray[14, k]);
string turma, disciplina;
string str = Convert.ToString(valueArray[j, k]);
if (Char.IsNumber(str[0]) && str.Contains(" "))
{
string[] splistring = str.Split(new[] { " " }, 2, StringSplitOptions.None);
turma = splistring[0];
disciplina = splistring[1];
}
else
{
turma = null;
disciplina = str;
}
context.Horarios.Add(new Horarios { Cod_Tempo = cod_Tempo, Dia_Semana = dia_Semana, Cod_Disciplina = ObterCodDisciplina(disciplina), Cod_Professor = ObterCodProfessor(nomeProfessor), Cod_Sala = ObterCodSala(sala), Cod_Turma = ObterCodTurma(turma) });
}
}
}
}
}
context.SaveChanges();
}
stopwatch.Stop();
MessageBox.Show("Done! Tempo: "+stopwatch.ElapsedMilliseconds);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private int? ObterCodDisciplina(string disciplina)
{
using (var context = new ScheduleDatabaseEntities())
{
var dis = context.Disciplinas.FirstOrDefault(a => a.Disciplina == disciplina);
if (dis == null)
{
var disciplinaEntity = new Disciplinas { Disciplina = disciplina };
context.Disciplinas.Add(disciplinaEntity);
context.SaveChanges();
return disciplinaEntity.Cod_Disciplina;
}
else
return dis.Cod_Disciplina;
}
}
private int? ObterCodProfessor(string professor)
{
using (var context = new ScheduleDatabaseEntities())
{
var prof = context.Professores.FirstOrDefault(a => a.Professor == professor);
if (prof == null)
{
var professorEntity = new Professores { Professor = professor };
context.Professores.Add(professorEntity);
context.SaveChanges();
return professorEntity.Cod_Professor;
}
else
return prof.Cod_Professor;
}
}
private int? ObterCodSala(string sala)
{
using (var context = new ScheduleDatabaseEntities())
{
var sal = context.Salas.FirstOrDefault(a => a.Sala == sala);
if (sal == null)
{
var salaEntity = new Salas { Sala = sala };
context.Salas.Add(salaEntity);
context.SaveChanges();
return salaEntity.Cod_Sala;
}
else
return sal.Cod_Sala;
}
}
private int? ObterCodTurma(string turma)
{
using (var context = new ScheduleDatabaseEntities())
{
if (turma == null)
return null;
var turm= context.Turmas.FirstOrDefault(a => a.Turma == turma);
if (turm == null)
{
var turmaEntity = new Turmas { Turma = turma };
context.Turmas.Add(turmaEntity);
context.SaveChanges();
return turmaEntity.Cod_Turma;
}
else
return turm.Cod_Turma;
}
}
It’s probably slow because you’re giving a
SaveChanges()
in the middle of the operation. In your current scenario, can you leave all this in one block? Both the insertion of schedules and disciplines?– Filipe Oliveira
I have to do Savechanges() in the method so that next time you see that he make the same query already be in the comic book, but I tested without Savechanges(), even though I knew it would not work as I wanted and still takes a long time.
– Exprove