/// <summary> /// Initial Process of the TPS /// will load the table schema /// </summary> public void Process() { _tpsTables = new TableSchemaCollection(); //read the header info ReadTPSHeaderInfo(); GetTPSPageInfo(); GetTPSTableSchema(); }
/// <summary> /// Will export data from all tables in the collection to a CSV /// </summary> /// <param name="tables"></param> /// <returns></returns> public bool ExportDataToCSV(TableSchemaCollection tableSchemas, string outputFolder) { if (tableSchemas.Count < 1) { return(true); //return empty set if we aren't actually searching for anything } //create a database for each of the schemas Dictionary <int, DataTable> dataTables = new Dictionary <int, DataTable>(); Dictionary <int, StreamWriter> outputFiles = new Dictionary <int, StreamWriter>(); foreach (TableSchema schema in tableSchemas.Values) { if (outputFiles.ContainsKey(schema.TableID)) { continue; } string newFile = outputFolder + "\\"; FileInfo fi = new FileInfo(_filename); if (fi.Extension.Length > 0) { newFile += fi.Name.Replace(fi.Extension, "-" + schema.TableName + ".CSV"); } else { newFile += fi.Name + "-" + schema.TableName + ".CSV"; } StreamWriter sw = new StreamWriter(newFile); outputFiles.Add(schema.TableID, sw); DataTable dt = schema.BuildEmptyDataTable(); string[] columnNames = dt.Columns.Cast <DataColumn>(). Select(column => "\"" + column.ColumnName.Replace("\"", "\"\"") + "\""). ToArray(); outputFiles[schema.TableID].WriteLine(string.Join(",", columnNames)); dataTables.Add(schema.TableID, dt); } try{ //1. Fetch all of the TPSRecords from the TPS file foreach (TPSPage page in _tpsPages) { List <TPSRecord> pageRecords = page.GetRecords(); foreach (TPSRecord record in pageRecords) { //go through each record to see if we need it if (record.RecordType == TPSRecord.TYPE_DATA) { //Check if this record belongs to a table that we are searchin for //Notice that we need to generate the table ID before we can check //This part is normally in the TableDataRecord file, but we want to know before //parsing the whole record in the name of efficiency byte[] recordTableIDBA = new byte[4]; if (record.RecordData.Length < 4) { continue; } //Get the tableID ( it is backwards ) recordTableIDBA[0] = record.RecordData[3]; recordTableIDBA[1] = record.RecordData[2]; recordTableIDBA[2] = record.RecordData[1]; recordTableIDBA[3] = record.RecordData[0]; int recordTableID = BitConverter.ToInt32(recordTableIDBA, 0); if (tableSchemas.ContainsKey(recordTableID)) { //we generate the record. This will actually create a datarow within the TDR Record.TableDataRecord tdr = new Record.TableDataRecord(record, tableSchemas[recordTableID]); //dataTables[recordTableID].Rows.Add(tdr.TableDataRow.ItemArray); string[] fields = tdr.TableDataRow.ItemArray.Select(field => "\"" + field.ToString().Replace("\"", "\"\"") + "\""). ToArray(); outputFiles[recordTableID].WriteLine(string.Join(",", fields)); } } } } }catch (Exception ex) { throw new Exception("Error exporting data: ", ex); } //attempt to close any open files foreach (StreamWriter sw in outputFiles.Values) { try{ sw.Flush(); sw.Close(); }catch (Exception) {} } return(true); }
/// <summary> /// Returns a dataset with one more datatables /// </summary> /// <param name="tableNames"></param> /// <returns></returns> public DataSet GetTableData(TableSchemaCollection tableSchemas) { DataSet retSet = new DataSet(); if (tableSchemas.Count < 1) { return(retSet); //return empty set if we aren't actually searching for anything } //create a database for each of the schemas Dictionary <int, DataTable> dataTables = new Dictionary <int, DataTable>(); foreach (TableSchema schema in tableSchemas.Values) { DataTable dt = schema.BuildEmptyDataTable(); dataTables.Add(schema.TableID, dt); } try{ //1. Fetch all of the TPSRecords from the TPS file foreach (TPSPage page in _tpsPages) { List <TPSRecord> pageRecords = page.GetRecords(); foreach (TPSRecord record in pageRecords) { //go through each record to see if we need it if (record.RecordType == TPSRecord.TYPE_DATA) { //Check if this record belongs to a table that we are searchin for //Notice that we need to generate the table ID before we can check //This part is normally in the TableDataRecord file, but we want to know before //parsing the whole record in the name of efficiency byte[] recordTableIDBA = new byte[4]; if (record.RecordData.Length < 4) { continue; } //Get the tableID ( it is backwards ) recordTableIDBA[0] = record.RecordData[3]; recordTableIDBA[1] = record.RecordData[2]; recordTableIDBA[2] = record.RecordData[1]; recordTableIDBA[3] = record.RecordData[0]; int recordTableID = BitConverter.ToInt32(recordTableIDBA, 0); if (tableSchemas.ContainsKey(recordTableID)) { //we generate the record. This will actually create a datarow within the TDR Record.TableDataRecord tdr = new Record.TableDataRecord(record, tableSchemas[recordTableID]); dataTables[recordTableID].Rows.Add(tdr.TableDataRow.ItemArray); } } } } }catch (Exception ex) { throw new Exception("Error searching for data: ", ex); } foreach (DataTable dt in dataTables.Values) { retSet.Tables.Add(dt); } return(retSet); }