private void buttonImport_Click(object sender, EventArgs e) { FormLoading formLoading = new FormLoading("正在导入,请稍后..."); formLoading.Show(); var worksheet = this.reoGridControlMain.Worksheets[0]; worksheet.EndEdit(new EndEditReason()); RemoveEmptyLines(worksheet); var result = this.MakeObjectByReoGridImport <TargetClass>(out int[] emptyLines, out string errorMessage); if (result == null) { formLoading.Close(); MessageBox.Show(errorMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } List <TargetClass> newObjs = result.ToList(); if (newObjs.Count == 0) { formLoading.Close(); MessageBox.Show("未导入任何数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Dictionary <string, string[]> unImportedColumns = new Dictionary <string, string[]>(); for (int i = 0; i < this.importVisibleKeyNames.Length; i++) { //如果在导入窗口中可见的列设置为不导入,则加入未导入列表中 if (this.importVisibleKeyNames[i].Import == false) { unImportedColumns.Add(this.importVisibleKeyNames[i].Key, this.GetColumn(i, emptyLines)); } } if (this.listImportHandler != null && this.listImportHandler(newObjs, unImportedColumns) == false) { formLoading.Close(); return; } new Thread(() => { WMSEntities wmsEntities = new WMSEntities(); //获取wmsEntities.XXX对象 PropertyInfo propertyOfTargetClass = wmsEntities.GetType().GetProperty(typeof(TargetClass).Name); if (propertyOfTargetClass == null) { throw new Exception("WMSEntities中不存在" + typeof(TargetClass).Name + "!"); } DbSet <TargetClass> dbSetOfTargetClass = propertyOfTargetClass.GetValue(wmsEntities, null) as DbSet <TargetClass>; //获取wmsEntities.XXX.Add()方法 MethodInfo methodAdd = typeof(DbSet <TargetClass>).GetMethod("Add"); foreach (var obj in newObjs) { methodAdd.Invoke(dbSetOfTargetClass, new object[] { obj }); } try { wmsEntities.SaveChanges(); } catch (Exception ex) { if (!this.IsDisposed) { this.Invoke(new Action(() => { formLoading.Close(); MessageBox.Show("导入失败,请检查网络连接\n请把如下错误信息反馈给我们:\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); })); } return; } if (!this.IsDisposed) { this.Invoke(new Action(() => { formLoading.Close(); this.importFinishedCallback?.Invoke(); MessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); })); } }).Start(); }