/// <summary> /// 导入985年度预算设备 /// </summary> /// <param name="httpContext"></param> /// <param name="postedFile"></param> /// <param name="request"></param> /// <param name="deviceReportID"></param> /// <returns></returns> public static string ImportConstructPlanDevice(this HttpContext httpContext, HttpPostedFile postedFile, HttpRequest request, int deviceReportID) { //return string.Empty; IDatabase database = Database.New(); string logName = "ConstructPlanDeviceImportLog" + DateTime.Now.ToLongDateString().ToString() + Guid.NewGuid().ToString(); var writer = httpContext.GetLogWriter(logName); string fileName = "ConstructPlanDevice"; DataSet ds = postedFile.GetDataFromUploadFile(httpContext, fileName); StringBuilder deviceImportLog = new StringBuilder(); StringBuilder deviceImportId = new StringBuilder(); int importDeviceCount = 0;//导入设备个数 foreach (DataRow row in ds.Tables[fileName].Rows) { string memberApplyItemID = row[0].ToString().Trim(); database = Database.New(); var reportItem = new DeviceReportItem(); try { /* 一个页面对应一个IDatabase,对于Linq来说,就是一个页面对应一个DataContext,这保证页面生存周期中的所有业务实体都来源或依附于同一个DataContext,避免了跨DataContext传递实体的问题。在Linq中,一个DataContext产生的Entity交由另一个DataContext中使用是一件非常麻烦的事情,必须保证实体必须使用Attach方法附加到新的DataContext上,如果不附加,则新的DataContext会认为该Entity是new出来的,这会导致再数据库中插入一条新的记录,而不是与现存记录建立关联,解决办法是不保存实体,而保存实体ID */ //保存设备条目的基本信息 reportItem.DeviceReport = database.DeviceReports.SingleOrDefault(dr => dr.ID == deviceReportID); var item = database.MemberApplyItems.SingleOrDefault(mai => mai.ID == Convert.ToInt16(memberApplyItemID));// request.GetEntity<MemberApplyItem>(database.MemberApplyItems, memberApplyItemID); if (item != null) reportItem.MemberApplyItem = item; reportItem.Save(database); importDeviceCount++; } catch (Exception e) { writer.WriteLine(string.Format("导入项目发生错误,项目名称为{0},错误信息为:{1}", memberApplyItemID, e.Message)); } deviceImportId.AppendFormat("{0},", reportItem.ID); } deviceImportLog.AppendFormat("共导入设备{0}个, 导入设备Id为:\n{1}\n", importDeviceCount, deviceImportId.ToString()); //projectImportLog.AppendFormat("项目成员匹配情况如下:\n{0}\n", expertMatchString.ToString()); writer.WriteLine(deviceImportLog.ToString()); httpContext.DeleteFile(postedFile.FileName); writer.Close(); return logName; }
public void DeviceImportSave() { var ids = Request.GetString("ids"); var IDs = ids.Split(','); var deviceReport = Request.GetDeviceReport(User, Database); var deviceReportInfo = string.Format("985办:{0},填写预算计划条目数据,年度:{1},经费来源:{2},ID:{3}", User.Name, deviceReport.ReportYear, deviceReport.FundSource, deviceReport.ID); BusinessLog.Write(User, UserIP, deviceReportInfo, this.GetType().FullName, "DeviceImportSave", Database); deviceReport.Save(Database); for (int i = 0; i < IDs.Length - 1; i++) { var a = Int32.Parse(IDs[i]); var memberApplyItem = Database.MemberApplyItems.GetByID(a); DeviceReportItem item = new DeviceReportItem(); item.DeviceReport = deviceReport; item.MemberApplyItem = memberApplyItem; item.Save(Database); } Response.Write("<html><body>{ success: true, LogDocumentName: '" + "" + "' }</body></html>"); }