I am building a program to insert excel file data into a database.
My database has this structure:
Tables (and fields):
- Schedule ( PK ), StartTime, EndTime, DayWeek, RoomId ( FK ), ClassId < 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 goal since code is to insert into the relational table if there is no field with that name already and associate it with the row to be filled. This way it works, but I wanted a way to improve performance, since this is a bit slow to execute the query, any suggestions?
Edit:
Complete Code, to realize the purpose 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;
}
}