public static void Import(string[] ofdFilenames) { int count = 0; using (SqlConnection connection = new SqlConnection(new SmartDB().Connection.ConnectionString)) { connection.Open(); foreach (var filename in ofdFilenames) { DataTable newMeter = MakeTable.Meter(filename); //bool flag = getMeterList.Code == ErrorEnum.NoError; DataTable fetchMeter = FetchTable.GetMeter(); var fMeter = new HashSet <string>(fetchMeter.AsEnumerable() .Select(x => x.Field <string>("SerialNumber"))); DataTable dtUniqueGateway = newMeter.AsEnumerable() .Where(x => !fMeter.Contains(x.Field <string>("SerialNumber"))) .CopyToDataTable(); InsertMeterBulkCopy(connection, dtUniqueGateway); } } }
public static void Import(string[] ofdFilenames) { int count = 0; using (SqlConnection connection = new SqlConnection(new SmartDB().Connection.ConnectionString)) { connection.Open(); foreach (var filename in ofdFilenames) { DataTable newGateway = MakeTable.Gateway(filename); //TODO Create temptable DataTable fetchGateway = FetchTable.GetGateway(); var fGateway = new HashSet <string>(fetchGateway.AsEnumerable() .Select(x => x.Field <string>("MacAddress"))); DataTable dtUniqueGateway = newGateway.AsEnumerable() .Where(x => !fGateway.Contains(x.Field <string>("MacAddress"))) .CopyToDataTable(); using (SqlBulkCopy s = new SqlBulkCopy(connection)) { s.DestinationTableName = "Gateway"; s.ColumnMappings.Add("MacAddress", "MacAddress"); s.ColumnMappings.Add("SimCard", "SimCard"); s.ColumnMappings.Add("X", "X"); s.ColumnMappings.Add("Y", "Y"); s.ColumnMappings.Add("Description", "Description"); s.ColumnMappings.Add("InstallationDate", "InstallationDate"); s.ColumnMappings.Add("MaintenanceDate", "MaintenanceDate"); s.ColumnMappings.Add("IPAddress", "IPAddress"); s.ColumnMappings.Add("DMZId", "DMZId"); s.ColumnMappings.Add("CityId", "CityId"); s.ColumnMappings.Add("Createdby", "Createdby"); s.ColumnMappings.Add("Editedby", "Editedby"); s.ColumnMappings.Add("DocDate", "DocDate"); s.ColumnMappings.Add("Show", "Show"); s.ColumnMappings.Add("LockCount", "LockCount"); try { s.WriteToServer(dtUniqueGateway); Console.WriteLine($"Importing was successful."); } catch (Exception ex) { Console.WriteLine($"Contact Admin: {ex.Message}", "Import"); } } } } }
public static void Import(string[] ofdFilenames, int max, ProgressBar ProgressBarImportStatus, Label LabelImported, Label LabelDuplicate) { int count = 0; using (SqlConnection connection = new SqlConnection(new SmartDB().Connection.ConnectionString)) { connection.Open(); LabelDuplicate.Invoke((Action) delegate { LabelDuplicate.Text = $"Number of duplicated CSV File/ s: {CSVDuplicate.Get(ofdFilenames).Count}"; }); foreach (var filename in ofdFilenames) { _gw = Path.GetFileName((Path.GetDirectoryName(filename))); _csvFilename = (Path.GetFileName(filename)); if (!CSVDuplicate.Get(ofdFilenames).Contains(_csvFilename)) { DataTable newMeterReading = MakeTable.RDS(filename); using (SqlBulkCopy s = new SqlBulkCopy(connection)) { s.DestinationTableName = "MeterReading"; s.ColumnMappings.Add("SerialNumber", "SerialNumber"); s.ColumnMappings.Add("ReadingDate", "ReadingDate"); s.ColumnMappings.Add("ReadingValue", "ReadingValue"); s.ColumnMappings.Add("LowBatteryAlr", "LowBatteryAlr"); s.ColumnMappings.Add("LeakAlr", "LeakAlr"); s.ColumnMappings.Add("MagneticTamperAlr", "MagneticTamperAlr"); s.ColumnMappings.Add("MeterErrorAlr", "MeterErrorAlr"); s.ColumnMappings.Add("BackFlowAlr", "BackFlowAlr"); s.ColumnMappings.Add("BrokenPipeAlr", "BrokenPipeAlr"); s.ColumnMappings.Add("EmptyPipeAlr", "EmptyPipeAlr"); s.ColumnMappings.Add("SpecificErr", "SpecificErr"); s.ColumnMappings.Add("MacAddress", "MacAddress"); try { s.WriteToServer(newMeterReading); Model.GatewayLog gatewayLog = new Model.GatewayLog() { RDS = 1, OMS = 0, GatewayMacAddress = _gw, CSVFilename = _csvFilename, CreatedBy = "2", EditedBy = "2", DocDate = DateTime.Now, Show = 1, LockCount = 0 }; //UPDATE METER _meter.GetMeterSyncMeterReading(new SmartDB()); var createGatewayLog = _gatewayLog.Create(new SmartDB(), ref gatewayLog); if (!MacDuplicate.Get(ofdFilenames).Contains(_gw)) { Model.Gateway gatewayC = new Model.Gateway() { MacAddress = Regex.Replace(_gw, @"^(..)(..)(..)(..)(..)(..)$", "$1:$2:$3:$4:$5:$6"), SimCard = null, X = 0, Y = 0, Description = null, InstallationDate = DateTime.Parse("06/20/1986"), MaintenanceDate = DateTime.Parse("06/20/1986"), Status = "Active", IPAddress = "192.0.0.1", DMZId = null, CityId = null, CreatedBy = "2", EditedBy = "2", DocDate = DateTime.Now, Show = 1, LockCount = 0 }; _gateway.Create(new SmartDB(), ref gatewayC); } //TODO ADD Meter Serial to Meter Table via stored proc REPORT METER_SYNCSERIALNUMBER_METERREADING bool flag = createGatewayLog.Code == ErrorEnum.NoError; if (flag) { //TODO ProgressBarImportStatus.Invoke((Action) delegate { ProgressBarImportStatus.Increment(1); }); count++; if (count == max) { //TODO //LabelImported.Invoke((Action)delegate //{ // LabelImported.Text = $"Number of imported CSV File/s: {count} Ok"; //}); _final = count; Console.WriteLine($"Importing was successful."); } } else { Console.WriteLine($"Contact Admin: Gateway log error", "Import"); } } catch (Exception ex) { Console.WriteLine($"Contact Admin: {ex.Message}", "Import"); } } } } } //TODO LabelImported.Invoke((Action) delegate { LabelImported.Text = $"Number of imported CSV File/s: {_final} Ok"; }); }