public int EnvelopeUpload(string envelopeTable, Envelope env, string _CONNSTR) { int execRezult = 0; try { string fileBodyParam1; string fileBodyParam2; byte[] fileBody; // IF FILE NOT FOUND : if (!File.Exists(env.fileLocation)) { fileBody = new byte[1]; fileBodyParam1 = ""; fileBodyParam2 = ""; } else { FileStream fs = new FileStream(env.fileLocation, FileMode.Open, FileAccess.Read); int fileSize = (int)fs.Length; fileBody = new byte[fileSize]; fs.Read(fileBody, 0, fileSize); fs.Close(); fileBodyParam1 = ", FILE_BODY"; fileBodyParam2 = ", @FILE_BODY"; } // CREATE TABLE[NBU_ENVELOPES] ( // [ID] INT NOT NULL IDENTITY(1000,1) PRIMARY KEY, // [FROM] nvarchar(15), // [TO] nvarchar(15), // [FILE_NAME] nvarchar(15), // [FILE_SIZE] INT, // [FILE_BODY] IMAGE, // [FILE_DATE] datetime, // [DATE_SENT] datetime, // [DATE_DELIV] datetime, // [ENV_NAME] nvarchar(15), // [ENV_PATH] nvarchar(255), // [SPRUSNBU_BANK_ID] INT, // // INSERT INTO NBU_ENVELOPES([FROM], [TO], [FILE_NAME], FILE_SIZE, FILE_BODY, FILE_DATE, DATE_SENT, DATE_DELIV, ENV_NAME, ENV_PATH, SPRUSNBU_BANK_ID) // VALUES('Sender1', 'Reciever1', 'FileName1', 333, '1/1/1', '1/1/1', '1/1/1', 'E_1_UYTX.ERT', 'Some-path', 1) string insertString = "INSERT INTO " + envelopeTable + " ([FROM], [TO], [FILE_NAME], [FILE_SIZE], [FILE_DATE], [DATE_SENT], [DATE_DELIV], [ENV_NAME], [ENV_PATH]," + " [SPRUSNBU_BANK_ID]" + fileBodyParam1 + ") VALUES" + " (@FROM, @TO, @FILE_NAME, @FILE_SIZE, @FILE_DATE, @DATE_SENT, @DATE_DELIV, @ENV_NAME, @ENV_PATH," + " @SPRUSNBU_BANK_ID" + fileBodyParam2 + ")"; string currentIdHost = env.sendFromAddress.Substring(env.sendFromAddress.Length - 4); // TODO : FULL REBUILD !!!!!!!! // TODO : FULL REBUILD !!!!!!!! // TODO : FULL REBUILD !!!!!!!! // TODO : FULL REBUILD !!!!!!!! string getIdForHost = "SELECT [ID] FROM SPRUSNBU_BANKS WHERE [IDHOST]='" + currentIdHost + "'"; using (SqlConnection sqlConn = new SqlConnection(_CONNSTR)) { sqlConn.Open(); int bankIdForHost = 0; // ID = 0 FOR NON-EXISTENT SENDER !!! using (SqlCommand cmd = new SqlCommand(getIdForHost, sqlConn)) { try { bankIdForHost = ((int)cmd.ExecuteScalar()); } catch (Exception exc) { // TODO : PROCEED ERRORS !!!!!!! // TODO : PROCEED ERRORS !!!!!!! // TODO : PROCEED ERRORS !!!!!!! // TODO : PROCEED ERRORS !!!!!!! } } using (SqlCommand cmd = new SqlCommand(insertString, sqlConn)) { cmd.Parameters.AddWithValue("@SPRUSNBU_BANK_ID", bankIdForHost); cmd.Parameters.AddWithValue("@FILE_BODY", fileBody); cmd.Parameters.AddWithValue("@FROM", env.sendFromAddress); cmd.Parameters.AddWithValue("@TO", env.recieveAddress); cmd.Parameters.AddWithValue("@FILE_NAME", env.fileName); cmd.Parameters.AddWithValue("@FILE_SIZE", Convert.ToInt32(env.fileSize)); cmd.Parameters.AddWithValue("@FILE_DATE", env.fileModified); cmd.Parameters.AddWithValue("@DATE_SENT", env.fileSent); cmd.Parameters.AddWithValue("@DATE_DELIV", env.fileDelivered); cmd.Parameters.AddWithValue("@ENV_NAME", env.envelopeName); cmd.Parameters.AddWithValue("@ENV_PATH", env.envelopePath); execRezult = cmd.ExecuteNonQuery(); sqlConn.Close(); } } } catch (Exception e) { execRezult = 0; MessageBox.Show(e.Message); } return execRezult; }
// UNPACK ENVELOPES AND UPLOAD INTO DB : private void ProcessEnvelopes() { string methodName = MethodInfo.GetCurrentMethod().Name; try { string todayUploadedLog = DateTime.Now.ToString("yyyyMMdd") + "_Uploaded.log"; // POSSIBLE A NEW DAY STARTED & NEW LOG FILE NEEDED : if (!File.Exists(todayUploadedLog)) { File.Create(todayUploadedLog); textBox_4_Tests_Only.Text = textBoxClearHeader; dataPicker.SelectedDate = DateTime.Now; } else { string dbLogin = passwordBoxLogin.Password.Trim(); string dbPassw = passwordBoxPassw.Password.Trim(); if (dbLogin.Length < 1 || dbPassw.Length < 1) { MessageBox.Show("Set Login & Password Before!"); } else { WorkWithDB_2015 workWithDB = new WorkWithDB_2015(_DATABASE, dbLogin, dbPassw); labelForTimer.Content = "Run Every " + timerMinutes + " min. Next Autorun at " + DateTime.Now.AddMinutes(timerMinutes).ToString("HH:mm:ss"); // "Next Autorun at - " + DateTime.Now.AddMinutes(15).ToShortTimeString(); // GET TODAY ENVELOPES FROM TODAY-FOLDER (OR ANOTHER SELECTED DATE) : FileInfo[] todayEnvelopes = GetEnvelopesListForDate(dataPicker.SelectedDate.Value); if (todayEnvelopes != null && todayEnvelopes.Length > 0) { for (int i = 0; i < todayEnvelopes.Length; i++) { // CREATE ENVELOPE USING EVERY ENVELOPE-FILE PARAMS : Envelope env = new Envelope(todayEnvelopes[i]); // TODO: SOMETHING WRONG... // TODO: SOMETHING WRONG... // TODO: SOMETHING WRONG... env.fileLocation = "FILE NOT FOUND !!!"; // CHECK IN LOG FILE IF NOT UPLOADED YET : if (!File.ReadAllText(todayUploadedLog).Contains(env.envelopeName)) { // GET UNPACKED FILES (LOOKING IN ALL POSSIBLE FOLDERS) : foreach (string possiblePath in possibleOutputDirs) { string outputFilePath = NbuRootDir + possiblePath + env.fileName; if (File.Exists(outputFilePath)) { env.fileLocation = outputFilePath; break; } } // START UPLOAD INTO DB HERE !!! : UploadEnvelope upload = new UploadEnvelope(); string connString = "Server=" + _SERVER + "; Database=" + _DATABASE + "; Uid=" + dbLogin + "; Pwd=" + dbPassw + ""; if (upload.EnvelopeUpload(_ENVELOPE_TBL, env, connString) != 0) // if (workWithDB.EnvelopeUpload(_ENVELOPE_TBL, env) != 0) // OLD 2015 VERSION; { // IT MEANS - UPLOADED SUCCESSFULLY ! File.AppendAllText(todayUploadedLog, env.envelopeName + " - " + env.fileName + Environment.NewLine); nLogger.Trace("{0}() - {1}", env.envelopeName, env.fileName); textBox_4_Tests_Only.Text += Environment.NewLine + DateTime.Now + " - " + env.envelopeName + " - " + env.fileName; } } } } nLogger.Trace("Job finished successfully."); textBox_4_Tests_Only.Text += Environment.NewLine + DateTime.Now + " - Ok."; } } } catch (Exception e) { nLogger.Error("{0}() - {1}", methodName, e.Message); } }
public int EnvelopeUpload(string envelopeTable, Envelope env) { int execRezult = 0; string methodName = MethodInfo.GetCurrentMethod().Name; try { string fileBodyParam1; string fileBodyParam2; byte[] fileBody; // IF FILE NOT FOUND : if (!File.Exists(env.fileLocation)) { fileBody = new byte[1]; fileBodyParam1 = ""; fileBodyParam2 = ""; nLogger.Error("{0}() - File Not Found {1} From Packet - {2}", methodName, env.fileLocation, env.envelopeName); } else { FileStream fs = new FileStream(env.fileLocation, FileMode.Open, FileAccess.Read); int fileSize = (int)fs.Length; fileBody = new byte[fileSize]; fs.Read(fileBody, 0, fileSize); fs.Close(); fileBodyParam1 = ", FILE_BODY"; fileBodyParam2 = ", @FILE_BODY"; } // [FROM] nvarchar(15), // [TO] nvarchar(15), // [FILE_NAME] nvarchar(15), // [FILE_SIZE] int, // [FILE_BODY] IMAGE, // [FILE_DATE] datetime, // [DATE_SENT] datetime, // [DATE_DELIV] datetime, // [ENV_NAME] nvarchar(15), // [ENV_PATH] nvarchar(255), string insertString = "INSERT INTO " + envelopeTable + " ([FROM], [TO], [FILE_NAME], [FILE_SIZE], [FILE_DATE], [DATE_SENT], [DATE_DELIV], [ENV_NAME], [ENV_PATH] " + fileBodyParam1 + ") VALUES" + " (@FROM, @TO, @FILE_NAME, @FILE_SIZE, @FILE_DATE, @DATE_SENT, @DATE_DELIV, @ENV_NAME, @ENV_PATH" + fileBodyParam2 + ")"; using (SqlConnection sqlConn = new SqlConnection(_CONNSTR)) { sqlConn.Open(); using (SqlCommand cmd = new SqlCommand(insertString, sqlConn)) { cmd.Parameters.AddWithValue("@FILE_BODY", fileBody); cmd.Parameters.AddWithValue("@FROM", env.sendFromAddress); cmd.Parameters.AddWithValue("@TO", env.recieveAddress); cmd.Parameters.AddWithValue("@FILE_NAME", env.fileName); cmd.Parameters.AddWithValue("@FILE_SIZE", Convert.ToInt32(env.fileSize)); cmd.Parameters.AddWithValue("@FILE_DATE", env.fileModified); cmd.Parameters.AddWithValue("@DATE_SENT", env.fileSent); cmd.Parameters.AddWithValue("@DATE_DELIV", env.fileDelivered); cmd.Parameters.AddWithValue("@ENV_NAME", env.envelopeName); cmd.Parameters.AddWithValue("@ENV_PATH", env.envelopePath); execRezult = cmd.ExecuteNonQuery(); sqlConn.Close(); } } } catch (Exception e) { execRezult = 0; nLogger.Error("{0}() - {1}", methodName, e.Message); } return execRezult; }