private void button1_Click(object sender, EventArgs e) { DataDAO dataDAO = new DataDAO(); HocPhan hocPhan = new HocPhan(); hocPhan.Ma_HP = textBox1.Text; hocPhan.Ten_HP = textBox2.Text; hocPhan.Ma_D = ""; hocPhan.Ma_SV = ""; hocPhan.ID_Khoa = MaKhoa(comboBox1.Text); dataDAO.Insert(hocPhan); Deletetextbox(); Close(); }
private void OnChanged(object sender, FileSystemEventArgs e) { //FileSystemWatcher.EnableRaisingEvents = false; foreach (string FullPath in System.IO.Directory.GetFiles(Path.GetDirectoryName(e.FullPath))) { try { while (!IsFileReady(System.IO.Directory.GetFiles(Path.GetDirectoryName(e.FullPath)).ToList())) { Thread.Sleep(2000); } DateTime FileNameDateTime = new DateTime(); DateTime FileExtensionDateTime = new DateTime(); DateTime ActualDateTime = new DateTime(); Console.WriteLine("Checking File name and extensions..."); try { if (FileType.Settings.UseFileName && !FileType.Settings.UseFileExtension) { ActualDateTime = DateTime.ParseExact(Path.GetFileNameWithoutExtension(FullPath).Replace(FileType.Settings.TextToIgnoreFileName, ""), FileType.Settings.DateTimeFormatFileName, CultureInfo.InvariantCulture); } if (FileType.Settings.UseFileExtension && !FileType.Settings.UseFileName) { ActualDateTime = DateTime.ParseExact(Path.GetExtension(FullPath).Replace(FileType.Settings.TextToIgnoreFileName, ""), FileType.Settings.DateTimeFormatFileExtension, CultureInfo.InvariantCulture); } if (FileType.Settings.UseFileName && FileType.Settings.UseFileExtension) { FileNameDateTime = DateTime.ParseExact(Path.GetFileNameWithoutExtension(FullPath).Replace(FileType.Settings.TextToIgnoreFileName, ""), FileType.Settings.DateTimeFormatFileName, CultureInfo.InvariantCulture); FileExtensionDateTime = DateTime.ParseExact(Path.GetExtension(FullPath).Replace(FileType.Settings.TextToIgnoreFileName, ""), FileType.Settings.DateTimeFormatFileExtension, CultureInfo.InvariantCulture); ActualDateTime = new DateTime(FileNameDateTime.Year, FileNameDateTime.Month, FileNameDateTime.Day, FileExtensionDateTime.Hour, FileExtensionDateTime.Minute, FileExtensionDateTime.Second); } } catch (Exception exception) { Console.WriteLine("Failed to get a DateTime from file. " + exception.Message); if (eventLog != null) { eventLog.WriteEntry("Failed to get a DateTime from file. " + exception.Message, EventLogEntryType.Error); } } if (FileType.Settings.TruncateTable) { try { DataDAO dataDAO = new DataDAO(CollectionDatabase); dataDAO.TruncateTable(FileType.DatabaseStoredProcedureName); eventLog.WriteEntry("Successfully truncated table " + FileType.DatabaseStoredProcedureName + ".", EventLogEntryType.Information); } catch (Exception exception) { Console.WriteLine("Failed to truncate table " + FileType.DatabaseStoredProcedureName + ". " + exception.Message); if (eventLog != null) { eventLog.WriteEntry("Failed to truncate table " + FileType.DatabaseStoredProcedureName + ". " + exception.Message, EventLogEntryType.Error); } } } string[] AllRowsInFile = File.ReadAllLines(FullPath); if (AllRowsInFile.Count() > 0) { string[] DataRows = AllRowsInFile.Skip(FileType.Headers.Count).ToArray(); foreach (string DataLine in DataRows) { int index = 0; string[] Data = DataLine.Split(FileType.CharacterDelimiter); foreach (Column column in FileType.Columns.OrderBy(x => x.ColumnNumber)) { try { if (!column.Ignore) { if (FileType.Settings.LinkDateTime && FileType.Settings.DateTimeColumn == column.ColumnName) { DateTime linkedDate = DateTime.ParseExact(Data[index], FileType.Settings.DateTimeFormatLinkDate, CultureInfo.InvariantCulture); if (linkedDate != null) { column.ColumnData = linkedDate; } //column.ColumnData = ActualDateTime; } else { column.ColumnData = Data[index]; } if (!column.NotInFile) { index++; } } else //Added { index++; } } catch (Exception exception) { Console.WriteLine("Failed to process row: " + DataLine + " - " + exception.Message); if (eventLog != null) { eventLog.WriteEntry("Failed to process row: LinkDate Format: " + FileType.Settings.DateTimeFormatLinkDate + ": " + DataLine + " - " + exception.Message, EventLogEntryType.Error); } } } if (CollectionDatabase != null) { try { DataDAO dataDAO = new DataDAO(CollectionDatabase); string debugData = ""; /* * foreach(Column column in FileType.Columns) * { * debugData += column.ColumnNumber + ":" + column.ColumnName + ":" + column.DatabaseColumnName + ":" + column.ColumnData + "\n"; * } */ //eventLog.WriteEntry("Attempting data: " + debugData, EventLogEntryType.Warning); dataDAO.Insert(FileType.DatabaseStoredProcedureName, FileType.Columns); } catch (Exception exception) { Console.WriteLine("Failed to insert row: " + DataLine + " - " + exception.Message); if (eventLog != null) { eventLog.WriteEntry("Failed to insert row: " + DataLine + " - " + exception.Message, EventLogEntryType.Error); } } } } } Console.WriteLine("Successfuly processed file: " + FullPath + "!"); if (eventLog != null) { eventLog.WriteEntry("Successfuly processed file: " + FullPath + "!", EventLogEntryType.Information); } } catch (Exception exception) { Console.WriteLine("Failed to prcoess file: " + FullPath + ". " + exception.Message); if (eventLog != null) { eventLog.WriteEntry("Failed to prcoess file: " + FullPath + ". " + exception.Message, EventLogEntryType.Error); } } try { if (File.Exists(Path.GetDirectoryName(FullPath) + @"\Processed\" + Path.GetFileName(FullPath))) { File.Delete(Path.GetDirectoryName(FullPath) + @"\Processed\" + Path.GetFileName(FullPath)); File.Move(FullPath, Path.GetDirectoryName(FullPath) + @"\Processed\" + Path.GetFileName(FullPath)); } else { File.Move(FullPath, Path.GetDirectoryName(FullPath) + @"\Processed\" + Path.GetFileName(FullPath)); } } catch (Exception exception) { Console.WriteLine("Failed to move file to Processed folder: " + FullPath + ". " + exception.Message); if (eventLog != null) { eventLog.WriteEntry("Failed to move file to Processed folder: " + FullPath + ". " + exception.Message, EventLogEntryType.Error); } } } //FileSystemWatcher.EnableRaisingEvents = true; }