需要帮助来微调查询

时间:2011-06-29 02:16:29

标签: c# performance

我有这个更新查询工作正常但只需要3-4秒才能成功获得消息框更新。你能帮忙看看出了什么问题吗?是因为using()和事务回滚吗?

public void Update()
    {
        System.Data.Common.DbTransaction transaction = null;
        using (JamminDataContext db = new JamminDataContext())
        {
            try
            {
                db.Connection.Open();
                transaction = db.Connection.BeginTransaction();
                db.Transaction = transaction;

                #region Update Users
                db.Users.Attach(this, GetSingleUserById(this.Id));
                db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, db.Users);
                db.SubmitChanges();
                #endregion

                if (this.RoleId == (int)RoleTypes.Student)
                {
                    #region Update CourseByStudents
                    foreach (CourseByStudent courseByStudent in this.courseByStudent)
                    {
                        if (courseByStudent == null) break;
                        if (courseByStudent.Id == 0)
                        {
                            courseByStudent.CourseUserStatus.UserId = this.Id;
                            db.CourseUserStatus.InsertOnSubmit(courseByStudent.CourseUserStatus);
                            db.SubmitChanges();

                            courseByStudent.StudentId = this.Id;
                            courseByStudent.CourseUserStatusId = courseByStudent.CourseUserStatus.Id;
                            db.CourseByStudents.InsertOnSubmit(courseByStudent);
                            db.SubmitChanges();
                        }
                        else
                        {
                            if(courseByStudent.CourseUserStatusCopy != courseByStudent.CourseUserStatus.Status
                                && ( courseByStudent.CourseUserStatus.Status != null
                                && courseByStudent.CourseUserStatus.Date != null))
                            {
                                //Insert to CourseUserStatus only when Status is change or add new row of course
                                courseByStudent.CourseUserStatus.UserId = this.Id;
                                db.CourseUserStatus.InsertOnSubmit(courseByStudent.CourseUserStatus);
                                db.SubmitChanges();

                                courseByStudent.CourseUserStatusId = courseByStudent.CourseUserStatus.Id;
                            }
                            courseByStudent.Update();
                        }
                    }
                    #endregion
                }

                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null) transaction.Rollback();
                Logger.Error(typeof(User), ex);
                throw;
            }
            finally
            {
                if (db.Connection.State == System.Data.ConnectionState.Open) db.Connection.Close();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

而不是在所有单独的db.SubmitChanges()之前,在tx.Commit()之前调用db.SubmitChanges()。如果这可以提高性能,请告诉我。它应该防止许多往返数据库,从而提高整体性能。