Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Fields? #70

Open
bbakermmc opened this issue Apr 15, 2019 · 4 comments
Open

Static Fields? #70

bbakermmc opened this issue Apr 15, 2019 · 4 comments

Comments

@bbakermmc
Copy link

Is there a way to add static fields?
EX: When loading from file and putting into the DB, we have a FileId that we generate before the insert, so we would want to add this, but it doesnt exist in the file. The code seems to blow up if I try to do it.

Field index must be included in [0, FieldCount[. Specified field index was : '16'.
Parameter name: field
Actual value was 16.

ref:

using (var reader = new CsvReader(new StreamReader(blobFile.OpenRead()), false, '|'))
               {

                   reader.MissingFieldAction = MissingFieldAction.ReplaceByNull;

                   reader.ParseError += (sender, args) =>
                   {
                       var error = args.Error;
                   };

                   var cols = new List<Column>();
                   cols.Add(new Column(){Name ="FileLogId", Type = typeof(string), OverrideValue = fileLogId.ToString(), DefaultValue = fileLogId.ToString()});

                   foreach (var field in fileDefinitionFields.Where(x => x.FieldOrder >0))
                   {
                       cols.Add(new Column(){Name = field.ColumnName, Type = typeof(string)});
                   }

                   reader.Columns = cols;

                   // Now use SQL Bulk Copy to move the data
                   using (var sbc = bulkCopy)
                   {


                       sbc.WriteToServer(reader);
                   }
               }
@jeddytier4
Copy link

I'm having a very similar issue. It appears that DefaultValue and OverrideValue are only applied when reading the CSV row by row. I need them to be applied for bulkcopy as well. I'm looking at the source, but can't see yet where the issue occurs.

@HTI-DaraiusKeeka
Copy link

Hi @bbakermmc
Was there any solution to this please?

@bbakermmc
Copy link
Author

@HTI-DaraiusKeeka Since there was no response looks like I ended up using a diff framework:
https://github.com/Cinchoo/ChoETL

REF:

private static void ChoEtlDelimitedMethod(CloudBlockBlob blobFile, int fileLogId, IEnumerable<FileDefinitionFieldsDto> fileDefinitionFields, SqlBulkCopy bulkCopy)
        {
            var config = new ChoCSVRecordConfiguration
                         {
                             FileHeaderConfiguration =
                             {
                                 HasHeaderRecord = true
                             }
                             , Delimiter                  = "|"
                             , AutoDiscoverColumns        = false
                             , AutoDiscoverFieldTypes     = false
                             , IsDynamicObject            = true
                             , ThrowAndStopOnMissingField = false
                         };

            foreach (var field in fileDefinitionFields.Where(x => x.FieldOrder > 0 && !x.IgnoreColumnFlagBool))
            {
                config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration(field.ColumnName, field.FieldOrder));
            }

            using (var parser = new ChoCSVReader(blobFile.OpenRead(), config).Select(row =>
                                                                             {
                                                                                 row.FileLogId = fileLogId.ToString();

                                                                                 return row;
                                                                             })
                                                                             .AsDataReader())
            {
                using (var bc = bulkCopy)
                {
                    bc.WriteToServer(parser);
                    bc.Close();
                    bulkCopy.Close();
                    bulkCopy = null;
                }
            }
        }

@HTI-DaraiusKeeka
Copy link

@HTI-DaraiusKeeka Since there was no response looks like I ended up using a diff framework:
https://github.com/Cinchoo/ChoETL

REF:

private static void ChoEtlDelimitedMethod(CloudBlockBlob blobFile, int fileLogId, IEnumerable<FileDefinitionFieldsDto> fileDefinitionFields, SqlBulkCopy bulkCopy)
        {
            var config = new ChoCSVRecordConfiguration
                         {
                             FileHeaderConfiguration =
                             {
                                 HasHeaderRecord = true
                             }
                             , Delimiter                  = "|"
                             , AutoDiscoverColumns        = false
                             , AutoDiscoverFieldTypes     = false
                             , IsDynamicObject            = true
                             , ThrowAndStopOnMissingField = false
                         };

            foreach (var field in fileDefinitionFields.Where(x => x.FieldOrder > 0 && !x.IgnoreColumnFlagBool))
            {
                config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration(field.ColumnName, field.FieldOrder));
            }

            using (var parser = new ChoCSVReader(blobFile.OpenRead(), config).Select(row =>
                                                                             {
                                                                                 row.FileLogId = fileLogId.ToString();

                                                                                 return row;
                                                                             })
                                                                             .AsDataReader())
            {
                using (var bc = bulkCopy)
                {
                    bc.WriteToServer(parser);
                    bc.Close();
                    bulkCopy.Close();
                    bulkCopy = null;
                }
            }
        }

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants