public void XmlExport(ReportLogger reportLogger) { int stepId = reportLogger.AddStep(); try { FileInfo fileInfo = new FileInfo(m_pathAndFileName); FileInfo serverPathAndFile = new FileInfo(ServerPath + fileInfo.Name); CoreDataLibrary.Data.Process.ExportItemToXml(ExportItem, serverPathAndFile); reportLogger.EndStep(stepId); } catch (Exception exception) { reportLogger.EndStep(stepId, exception); } }
public static void FtpFile(FileInfo fileToFtp, string ftpTarget) { ReportLogger ftpFileReportLogger = new ReportLogger("FtpFile"); int stepId = ftpFileReportLogger.AddStep(); try { //ftpUploadUri = "ftp://ftp.lowcosttravelgroup.com/BusinessRules/"; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpTarget + fileToFtp.Name); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("XTGtravelgate", "LcH3rdparty"); request.Timeout = 60 * 10000; FileStream fileStream = new FileStream(fileToFtp.FullName, FileMode.Open, FileAccess.Read); Stream requestStream = request.GetRequestStream(); byte[] buffer = new byte[8092]; int read = 0; while ((read = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, read); } requestStream.Flush(); requestStream.Close(); fileStream.Close(); ftpFileReportLogger.EndStep(stepId); } catch (Exception e) { ftpFileReportLogger.EndStep(stepId, e); } ftpFileReportLogger.EndLog(); }
private void CreateCsvFile(FileInfo tempDirectory, string filename, FileInfo fileInfo, StringBuilder headers, FileInfo serverPathAndFile, FileInfo serverPath, string languageEncoding) { int stepId = PropertyExporterLogger.AddStep(); try { //CoreDataLibrary.Data.Process.ExportItemToXml(ExportItem, serverPathAndFile); //CsvToXmlParser xmlParser = new CsvToXmlParser(serverPathAndFile); string line; if (File.Exists(tempDirectory + filename + TEMPFILE_APPENDING)) { File.Delete(tempDirectory + filename + TEMPFILE_APPENDING); } StreamWriter writer = null; StreamReader reader = null; FileStream fs = new FileStream(tempDirectory + fileInfo.Name, FileMode.Open); FileStream outStream = new FileStream(tempDirectory + filename + TEMPFILE_APPENDING, FileMode.Append); reader = new StreamReader(fs, Encoding.Default); string encoding = Get.GetLanguageEncoding(languageEncoding); if (String.IsNullOrEmpty(encoding)) { writer = new StreamWriter(outStream, Encoding.GetEncoding("windows-1252")); } else { writer = new StreamWriter(outStream, Encoding.GetEncoding(encoding)); } writer.WriteLine(headers); string[] stringSeperators = { "(!!!!!!!!!!)" }; string[] fieldSeperators = { "!|!" }; string lastEntry = ""; while ((line = reader.ReadLine()) != null) { string[] lines = line.Split(stringSeperators, StringSplitOptions.RemoveEmptyEntries); if (lines.Length > 0) { for (int i = 0; i <= lines.Length - 1; i++) { if (lines[i].Split(fieldSeperators, StringSplitOptions.None).Length - 1 == headers.ToString().Split('|').Length - 1) { string row = lines[i].Replace("!|!", "|"); writer.WriteLine(row); } else { lastEntry = lastEntry + lines[i]; if (lastEntry.Split(fieldSeperators, StringSplitOptions.None).Length - 1 == headers.ToString().Split('|').Length - 1) { lastEntry = lastEntry.Replace("!|!", "|"); writer.WriteLine(lastEntry); lastEntry = ""; } } } } } writer.Flush(); reader.Close(); writer.Close(); string pathAndFileToFtp = ""; File.Delete(tempDirectory + fileInfo.Name); if (File.Exists(serverPathAndFile.FullName)) { File.Delete(serverPathAndFile.FullName); } File.Move(tempDirectory + filename + TEMPFILE_APPENDING, serverPathAndFile.FullName); pathAndFileToFtp = ExportItem.ExportItemName + ".csv"; File.Delete(tempDirectory + filename + TEMPFILE_APPENDING); if (CoreDataLib.IsLive()) { if (ExportItem.ExportItemFtpId > 0) { FtpExportedFile(pathAndFileToFtp); } } PropertyExporterLogger.EndStep(stepId); } catch (Exception exception) { PropertyExporterLogger.EndStep(stepId, exception); } }
public void CsvExport(ReportLogger reportLogger) { int stepId = reportLogger.AddStep(); try { FileInfo fileInfo = new FileInfo(m_pathAndFileName); FileInfo serverPathAndFile = new FileInfo(ServerPath + fileInfo.Name); FileInfo serverPath = new FileInfo(ServerPath); FileInfo tempDirectory = new FileInfo(ServerPath + @"Temp\"); string filename = fileInfo.Name.Split('.')[0]; ServerPathAndFile = serverPathAndFile.FullName; StringBuilder headers = new StringBuilder(); logger.AppendLine("Create Headers"); foreach (string header in m_selectStatementBuilder.GetOutputHeaders) { headers.Append(header + "|"); } headers.Remove(headers.Length - 1, 1); logger.AppendLine("Headers Created"); using (SqlConnection conn = new SqlConnection(DataConnection.SqlConnCoreData)) { SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandTimeout = 60000; sqlCommand.Connection = conn; StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@"DECLARE @bcpCmd as VARCHAR(8000);"); sqlBuilder.Append(@"SET @bcpCmd= 'bcp ""SET NOCOUNT ON;"); sqlBuilder.Append(m_selectClause); sqlBuilder.Append(@";SET NOCOUNT OFF;"""); sqlBuilder.Append(" queryout "); sqlBuilder.Append(ExportServerTemp + fileInfo.Name); sqlBuilder.Append(" -t \"" + m_del + "\" -r(!!!!!!!!!!)"); if (CoreDataLib.IsLive()) { sqlBuilder.Append(" -c -C ACP -S SVRsql4 -d CoreData -U CoreData -P CoreD@T@'; EXEC master..xp_cmdshell @bcpCmd;"); } else { sqlBuilder.Append(" -c -C ACP -S SVRsql4 -d CoreData_Test -U CoreData -P CoreD@T@'; EXEC master..xp_cmdshell @bcpCmd;"); } sqlBuilder.Replace("@LangId", m_selectStatementBuilder.LanguageId); string sql = sqlBuilder.ToString(); sqlCommand.CommandText = sql; conn.Open(); sqlCommand.ExecuteNonQuery(); if (File.Exists(tempDirectory + fileInfo.Name)) { CreateCsvFile(tempDirectory, filename, fileInfo, headers, serverPathAndFile, serverPath, m_selectStatementBuilder.LanguageId); } else { Emailer.SendEmail("*****@*****.**", "CsvDataExporter [SVRSQL4]: - Error : ", "File not found : " + tempDirectory);// + fileInfo.Name + " : " + DateTime.Now.ToLongDateString() + " : " + DateTime.Now.ToLongTimeString()); } reportLogger.EndStep(stepId); // StringBuilder sqlBuilder = new StringBuilder(); // sqlBuilder.Append(m_selectClause); // sqlBuilder.Replace("@LangId", m_selectStatementBuilder.LanguageId); // string sql = sqlBuilder.ToString(); // sqlCommand.CommandText = sql; // conn.Open(); // DataTable dataTable = new DataTable(); // dataTable.Load(sqlCommand.ExecuteReader()); // foreach (DataRow row in dataTable.Rows) // { // for (int i = 0; i <= dataTable.Columns.Count - 1; i++) // { // if (row[i].ToString().Contains("|")) // { // string newValue = row[i].ToString().Replace("|", ""); // row[i] = newValue; // } // } // } // if (File.Exists(tempDirectory + fileInfo.Name)) // { ////CsvToXmlParser xmlParser = new CsvToXmlParser(serverPathAndFile); // CoreDataLib.CreateCSVfile(dataTable, serverPathAndFile.FullName, "|"); // } // else // { // Emailer.SendEmail("*****@*****.**", "CsvDataExporter [SVRSQL4]: - Error : ", "File not found : " + tempDirectory);// + fileInfo.Name + " : " + DateTime.Now.ToLongDateString() + " : " + DateTime.Now.ToLongTimeString()); // } // reportLogger.EndStep(stepId); } } catch (Exception exception) { reportLogger.EndStep(stepId, exception); } }