public bool insert(IEnumerable <FileUpload> fileuploads) { bool result = false; try { using (var ctx = new FileUploadDbEntities()) { ctx.FileUpload.AddRange(fileuploads); ctx.SaveChanges(); result = true; } } catch (Exception ex) { throw new Exception(ex.Message); } return(result); }
public static FileProcessInfo StartProcess(FileProcessInfo startInfo, Stream dataStream) { if (startInfo == null) { throw new ArgumentNullException(nameof(startInfo)); } if (dataStream == null) { throw new ArgumentNullException(nameof(dataStream)); } //todo dir name var dirName = Environment.ExpandEnvironmentVariables("%temp%\\FileUploadManager"); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } var tempFileName = Path.Combine(dirName, startInfo.UniqueId.ToString()); try { using (var fs = new FileStream(tempFileName, FileMode.Create, FileAccess.Write)) { var buffer = new byte[64 * 1024]; int readCount; while ((readCount = dataStream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, readCount); } } using (var db = new FileUploadDbEntities()) { db.Connection.Open(); using (var tran = db.Connection.BeginTransaction()) { var fileInfo = new EFDataFileInfo(); fileInfo.CreationTime = startInfo.FileInfo.CreationTime; fileInfo.Hash = startInfo.FileInfo.Hash; fileInfo.HostName = Environment.MachineName; fileInfo.Name = startInfo.FileInfo.Name; fileInfo.Path = tempFileName; fileInfo.Size = startInfo.FileInfo.Size; //todo: GETDATE() fileInfo.InsertTime = DateTime.Now; EFFileProcessInfo processInfo = new EFFileProcessInfo(); processInfo.DataFileInfo = fileInfo; processInfo.StartTime = startInfo.StartTime; processInfo.UniqueId = startInfo.UniqueId; processInfo.State = (byte)ProcessState.Parsing; db.AddToEFFileProcessInfoes(processInfo); db.SaveChanges(); tran.Commit(); startInfo.Id = processInfo.Id; startInfo.State = (ProcessState)processInfo.State; return(startInfo); } } } catch (Exception e) { try { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } catch (Exception innerException) { //todo use FT.Common e.Data["InnerException"] = innerException.ToString(); } throw; } }