private static void validateAndCompressTicketData(ticketComments currentTicket) { bool found = false; int index = comments.Count; if (index > 0) { if (comments[comments.Count - 1].ticketNumber == currentTicket.ticketNumber) { comments[comments.Count - 1].comments.Add(currentTicket.comments[0]); found = true; } } if (!found) { comments.Add(currentTicket); } }
private static void selectTroubleNotes() { SqlConnection conn = new SqlConnection(connString); SqlCommand command; SqlDataReader reader; try { command = new SqlCommand(@" SELECT * FROM [COMMSOFT].[CCTS].[COMDB200].[TBNPHY01] order by J6DQNR, J6I6NS", conn); command.CommandTimeout = 600; command.CommandType = CommandType.Text; conn.Open(); reader = command.ExecuteReader(); comments = new List<ticketComments>(); int count = 0; if (reader.HasRows) { while (reader.Read()) { count++; Console.WriteLine(String.Format("Parsing row {0} / {1}", count, tableSizeCount)); ticketComments ticket = new ticketComments(); int ticketNumber = -1; int index = -1; string comment = ""; KeyValuePair<int, string> commentKVP = new KeyValuePair<int, string>(); //field 0 is an auto increment field. ignoring it. for (int i = 0; i < reader.FieldCount; i++) { if (reader.GetName(i) == "J6DQNR") { string value = reader.GetSqlValue(i).ToString(); if (!int.TryParse(value, out ticketNumber)) { ticketNumber = -1; Console.WriteLine(String.Format("Couldn't parse ticketNumber {0}...this should never happen!!!!!!!!!!!!!!", value)); } else { ticket.ticketNumber = ticketNumber; } } if (reader.GetName(i) == "J6I6NS") { string value = reader.GetSqlValue(i).ToString(); if (!int.TryParse(value, out index)) { index = -1; Console.WriteLine(String.Format("Couldn't parse index {0}...this should never happen!!!!!!!!!!!!!!", value)); } else { ticket.index = index; } } if (reader.GetName(i) == "J6MAXU") { comment = reader.GetSqlValue(i).ToString().Trim(); } } commentKVP = new KeyValuePair<int, string>(index, comment); ticket.comments.Add(commentKVP); //Compress / add the new ticket... validateAndCompressTicketData(ticket); } } //Truncate the database truncateDatabase("[Almond].[DBO].[TBNPHY01_COPY]"); //insert to the database insertTroubleNotesToDatabase(comments); conn.Close(); Console.Clear(); Console.WriteLine("Parse Complete"); } catch (Exception e) { MessageBox.Show(String.Format("exception {0}", e)); } }