public string Post(string put, [FromBody] Needs needs) { var ret = "提交失败,请联系系统管理员:{0}"; DataSet ds; DateTime now = DateTime.Now; using (var svc = new SqlFace.Models.OrmService(Config.AppConfigs.sqlfaceconn)) { var t = svc.GetById <Imp_Management>(needs.guid); if (svc.FilterWhere <Imp_4g_Log>(s => s.import_id == t.id).Count > 0) { return(string.Format("不能重复导入多次")); } construction(t.phy_path, out ds); using (IDbTransaction trans = svc.theConnection.BeginTransaction(IsolationLevel.ReadCommitted)) { try { string nbr, lvl; DataTable dt = ds.Tables[0]; t.addHeads = needs.addHeads; t.mem = needs.mem; t.last_modified_date = now; svc.Update <Imp_Management>(t, new List <string> { "addHeads", "mem", "last_modified_date" }, s => s.id == t.id); if (needs.addHeads) { nbr = dt.Columns[needs.indexes[0]].ColumnName; lvl = dt.Columns[needs.indexes[1]].ColumnName; svc.Create <Imp_4g_Log>(new Imp_4g_Log { log_id = Guid.NewGuid().ToString(), import_id = t.id, phone_number = nbr, pn_level_id = lvl, create_date = now, commit_date = null, old_pn_level_id = null, remarks = null, rsc_cd = null }); } foreach (DataRow dr in dt.Rows) { nbr = dr[needs.indexes[0]].ToString(); lvl = dr[needs.indexes[1]].ToString(); svc.Create <Imp_4g_Log>(new Imp_4g_Log { log_id = Guid.NewGuid().ToString(), import_id = t.id, phone_number = nbr, pn_level_id = lvl, create_date = now, commit_date = null, old_pn_level_id = null, remarks = null, rsc_cd = null }); } } catch (Exception e) { trans.Rollback(); return(string.Format("导入文件中存在重复号码", e.Message)); } trans.Commit(); try { Dictionary <string, OracleParameter> cmdp = new Dictionary <string, OracleParameter>(); cmdp.Add("import_id", new OracleParameter("sql_text", OracleDbType.Varchar2, 400, t.id, System.Data.ParameterDirection.Input)); cmdp.Add("returnstr", new OracleParameter("returnstr", OracleDbType.Varchar2, 400, null, System.Data.ParameterDirection.Output)); cmdp.Add("stats", new OracleParameter("stats", OracleDbType.Varchar2, 400, null, System.Data.ParameterDirection.Output)); Utils.DBTool.RunOracleProcedure("hbwh_oss_admin.Import_4gNbr_with_lvl", ref cmdp); var returnstr = (null == cmdp["returnstr"].Value) ? null : cmdp["returnstr"].Value.ToString(); var stats = (null == cmdp["stats"].Value) ? null : cmdp["stats"].Value.ToString(); if (returnstr == null || returnstr.ToLower() == "null") { return("提交成功," + stats); } else { return(string.Format(ret, returnstr)); } } catch { try //调用异常,回滚数据,让用户可以再次尝试 { var rt = Utils.DBTool.runCommands(new List <string> { string.Format("delete imp_log where import_id = '{0}'", t.id) }); throw; } catch { throw; }; } } } }
public Task <object> Post() { string password; if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!")); } try { var httpRequest = HttpContext.Current.Request; var keys = httpRequest.Form; password = keys["password"]; if (!checkpassword(password)) { throw new ApplicationException("密码错误!"); } } catch (Exception e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Forbidden, e.Message)); } try { var streamProvider = new MyMultipartFormDataStreamProvider("importing/uploads", Request.RequestUri.Authority); //Request.Content.LoadIntoBufferAsync().Wait(); return(Request.Content.ReadAsMultipartAsync(streamProvider). ContinueWith <object>(res => { object json; var context = res.Result.FileData.Select(i => { DataSet ds; List <String> cols; List <Object[]> data; List <Type> colTypes; var phy_path = i.LocalFileName.Replace("\"", ""); var rname = i.Headers.ContentDisposition.FileName; var test = i.Headers.ContentDisposition.ModificationDate; try { construction(phy_path, out ds); using (ds) { SqlFace.Models.DynmicService.GetTableRowsDataCompact(ds.Tables[0], out cols, out data, out colTypes); } FileInfo df = res.Result.fnmap[rname]; var guid = Guid.NewGuid().ToString(); var svc = new SqlFace.Models.OrmService(Config.AppConfigs.sqlfaceconn); svc.Create(new Imp_Management { id = guid, create_date = df.CreationTime, file_name = rname.Trim('"'), length = df.Length, uploader = "anonymous", last_modified_date = df.LastWriteTime, phy_path = phy_path, addHeads = null, total = data.Count }); svc.Dispose(); json = new { credit = guid, cols = cols, data = data, colTypes = colTypes, total = data.Count }; return json; } catch (Exception e) { json = new { hasError = true, Message = e.Message }; return json; } }); return context; })); } catch (Exception e) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message)); } }