将行插入关系表的最佳方法

时间:2015-03-31 00:07:51

标签: c# sql-server excel linq entity-framework

我在单击按钮事件中包含此代码:

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 teacherName = 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 timeId = Convert.ToInt32(valueArray[j, 1]);
                                string startTime = Convert.ToString(valueArray[j, 2]);
                                string endTime = Convert.ToString(valueArray[j, 3]);
                                string room = Convert.ToString(valueArray[j, k + 1]);
                                string weekDay= Convert.ToString(valueArray[14, k]);
                                string sClass, subject;
                                string str = Convert.ToString(valueArray[j, k]);

                                if (Char.IsNumber(str[0]) && str.Contains(" "))
                                {
                                    string[] splistring = str.Split(new[] { " " }, 2, StringSplitOptions.None);
                                    sClass = splistring[0];
                                    subject = splistring[1];
                                }
                                else
                                {
                                    sClass= null;
                                    subject = str;
                                }
                                context.Schedule.Add(new Schedule { TimeId= timeId , WeekDay= weekDay, SubjectId= FindSubjectId(subject), TeacherId= FindTeacherId(teacherName), RoomId= FindRoomId(room), ClassId= FindClassId(sClass) });
                            }
                        }
                    }
                }
            }
            context.SaveChanges();
        }
        stopwatch.Stop();
        MessageBox.Show("Done! Time: "+stopwatch.ElapsedMilliseconds);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

private int? FindSubjectId(string subject)
{
    using (var context = new ScheduleDatabaseEntities())
    {
        var findSubject = context.Subjects.FirstOrDefault(a => a.Subject == subject);
        if (findSubject == null)
        {
            var subjectEntity = new Subjects { Subject= subject};
            context.Subjects.Add(subjectEntity);
            context.SaveChanges();
            return subjectEntity.SubjectId;
        }
        else
            return findSubject.SubjectId;
    }
}

private int? FindTeacherId(string teacher)
{
    using (var context = new ScheduleDatabaseEntities())
    {
        var findTeacher = context.Professores.FirstOrDefault(a => a.Professor == professor);
        if (findTeacher == null)
        {
            var teacherEntity = new Teachers{ Teacher = teacher};
            context.Professores.Add(teacherEntity);
            context.SaveChanges();
            return teacherEntity.TeacherId;
        }
        else
            return findTeacher.TeacherId;
    }
}

private int? FindRoomId(string room)
{
    using (var context = new ScheduleDatabaseEntities())
    {
        var findRoom = context.Rooms.FirstOrDefault(a => a.Room == room);
        if (findRoom == null)
        {
            var roomEntity = new Rooms{ Room = room};
            context.Rooms.Add(roomEntity);
            context.SaveChanges();
            return roomEntity.RoomId;
        }
        else
            return findRoom.RoomId;
    }
}

private int? FindClassId(string sClass)
{
    using (var context = new ScheduleDatabaseEntities())
    {
        if (sClass == null)
            return null;
        var findClass = context.Turmas.FirstOrDefault(a => a.Turma == turma);
        if (findClass  == null)
        {
            var classEntity = new Classes { Class = sClass};
            context.Classes.Add(classEntity);
            context.SaveChanges();
            return classEntity.ClassId;
        }
        else
            return findClass.ClassId;
    }
}

问题是执行此操作比单个表插入(这是我之前的版本,但我想做关系表对于更好的组织更好),我执行此操作比预期多一点,我知道插入速度会慢一些不同的表格,但没有任何解决方案可以让它更快一点?

注意:我们的想法是从每位教师那里读取一些日程安排文件(在excel中),然后将它们检索到数据库,以便从中进行课程安排。我已经有了要插入数据库的值,所以我必须在关系表中插入并用该ID填充行。我已经有了一个快速的版本,但只有一个表,这是一个糟糕的设计(它仅用于测试目的)。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

从应用程序开始到结束时保持您的上下文,然后在应用程序启动时调用每个dbset上的Load。这会将所有数据加载到dbset对象的local属性中。然后你可以在该列表上使用linq因为我相信调用context.Salas。将导致EF查询数据库并生成较慢的对象。此外,您不必在按钮单击上创建新的上下文。

//app start:
var context = new ScheduleDatabaseEntities();
context.Subjects.Load();

private async void ButtonEvent(...)
{
    int result = await FindSubjectId();
}

//button code
private async Task<int?> FindSubjectId(string subject)
{
        var findSubject = context.Subjects.Local.FirstOrDefault(a => a.Subject == subject);
        if (findSubject == null)
        {
            var subjectEntity = new Subjects { Subject= subject};
            context.Subjects.Add(subjectEntity);
            await context.SaveChangesAsync();
            return subjectEntity.SubjectId;
        }
        else
            return findSubject.SubjectId;
    }
这样的事情。如果需要,你可以让FindSubjectId异步无效,只需在按钮事件中正常调用它。