{"id":905,"date":"2015-06-11T11:27:47","date_gmt":"2015-06-11T15:27:47","guid":{"rendered":"http:\/\/binaryworld.net\/blogs\/?p=905"},"modified":"2015-12-01T16:37:34","modified_gmt":"2015-12-01T21:37:34","slug":"get-list-of-flat-file-columns-in-ssis","status":"publish","type":"post","link":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/","title":{"rendered":"Get File Column Count using SSIS C# Script Task"},"content":{"rendered":"<p>Sometimes you have changing schema in your flat file (Columns added or Removed)<\/p>\n<p>If you ever want to know how many columns you have in flat file then simply use below snippet and it will tell you column count.<\/p>\n<figure style=\"width: 538px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/binaryworld.net\/blogs\/images\/ssis\/count-file-columns-ssis-csharp.png\" alt=\"How to count columns in Flat file using C# in SSIS\" width=\"538\" height=\"288\" \/><figcaption class=\"wp-caption-text\">How to count columns in Flat file using C# in SSIS<\/figcaption><\/figure>\n<pre class=\"brush: csharp; gutter: true\">#region Namespaces\r\nusing System;\r\nusing System.Data;\r\nusing Microsoft.SqlServer.Dts.Runtime;\r\nusing System.Windows.Forms;\r\n#endregion\r\n\r\nnamespace ST_e266a0b09e3c48eea5a73ff6fe09a353\r\n{\r\n    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]\r\n    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase\r\n    {\r\n        public void Main()\r\n        {\r\n            var colCnt = GetFileColumns(&quot;file1&quot;);\r\n            MessageBox.Show(&quot;Your file has total &quot; + colCnt + &quot; columns&quot;);\r\n            Dts.TaskResult = (int)ScriptResults.Success;\r\n        }\r\n        private int GetFileColumns(string connName,char colSeperator= &#039;,&#039;)\r\n        {\r\n            var filePath=Dts.Connections[connName].ConnectionString;\r\n            string firstline;\r\n            using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))\r\n            {\r\n                 firstline= sr.ReadLine();\r\n            }              \r\n            var colArr=firstline.Split(new char[]{colSeperator});\r\n            return colArr.Length;\r\n        }\r\n        #region ScriptResults declaration\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ This enum provides a convenient shorthand within the scope of this class for setting the\r\n        \/\/\/ result of the script.\r\n        \/\/\/ \r\n        \/\/\/ This code was generated automatically.\r\n        \/\/\/ &lt;\/summary&gt;\r\n        enum ScriptResults\r\n        {\r\n            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,\r\n            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure\r\n        };\r\n        #endregion\r\n\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you have changing schema in your flat file (Columns added or Removed) If you ever want to know how many columns you have in flat file then simply use below snippet and it will tell you column count. #region &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/\"> <span class=\"screen-reader-text\">Get File Column Count using SSIS C# Script Task<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":906,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[52,142,143,30],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>Get File Column Count using SSIS C# Script Task - BinaryWorld Blog<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Get File Column Count using SSIS C# Script Task - BinaryWorld Blog\" \/>\r\n<meta property=\"og:description\" content=\"Sometimes you have changing schema in your flat file (Columns added or Removed) If you ever want to know how many columns you have in flat file then simply use below snippet and it will tell you column count. #region &hellip; Get File Column Count using SSIS C# Script Task Read More &raquo;\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/\" \/>\r\n<meta property=\"og:site_name\" content=\"BinaryWorld Blog\" \/>\r\n<meta property=\"article:published_time\" content=\"2015-06-11T15:27:47+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2015-12-01T21:37:34+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png\" \/>\r\n\t<meta property=\"og:image:width\" content=\"538\" \/>\r\n\t<meta property=\"og:image:height\" content=\"288\" \/>\r\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\r\n<meta name=\"author\" content=\"Binary World\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binary World\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/\",\"url\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/\",\"name\":\"Get File Column Count using SSIS C# Script Task - BinaryWorld Blog\",\"isPartOf\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png\",\"datePublished\":\"2015-06-11T15:27:47+00:00\",\"dateModified\":\"2015-12-01T21:37:34+00:00\",\"author\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\"},\"breadcrumb\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#primaryimage\",\"url\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png\",\"contentUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png\",\"width\":538,\"height\":288},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryworld.net\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get File Column Count using SSIS C# Script Task\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\",\"url\":\"https:\/\/binaryworld.net\/blogs\/\",\"name\":\"BinaryWorld Blog\",\"description\":\"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\",\"name\":\"Binary World\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"caption\":\"Binary World\"},\"description\":\"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.\",\"url\":\"https:\/\/binaryworld.net\/blogs\/author\/admin\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get File Column Count using SSIS C# Script Task - BinaryWorld Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/","og_locale":"en_US","og_type":"article","og_title":"Get File Column Count using SSIS C# Script Task - BinaryWorld Blog","og_description":"Sometimes you have changing schema in your flat file (Columns added or Removed) If you ever want to know how many columns you have in flat file then simply use below snippet and it will tell you column count. #region &hellip; Get File Column Count using SSIS C# Script Task Read More &raquo;","og_url":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/","og_site_name":"BinaryWorld Blog","article_published_time":"2015-06-11T15:27:47+00:00","article_modified_time":"2015-12-01T21:37:34+00:00","og_image":[{"width":538,"height":288,"url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png","type":"image\/png"}],"author":"Binary World","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binary World","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/","url":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/","name":"Get File Column Count using SSIS C# Script Task - BinaryWorld Blog","isPartOf":{"@id":"https:\/\/binaryworld.net\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#primaryimage"},"image":{"@id":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#primaryimage"},"thumbnailUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png","datePublished":"2015-06-11T15:27:47+00:00","dateModified":"2015-12-01T21:37:34+00:00","author":{"@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0"},"breadcrumb":{"@id":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#primaryimage","url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png","contentUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2015\/06\/count-file-columns-ssis-csharp.png","width":538,"height":288},{"@type":"BreadcrumbList","@id":"https:\/\/binaryworld.net\/blogs\/get-list-of-flat-file-columns-in-ssis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryworld.net\/blogs\/"},{"@type":"ListItem","position":2,"name":"Get File Column Count using SSIS C# Script Task"}]},{"@type":"WebSite","@id":"https:\/\/binaryworld.net\/blogs\/#website","url":"https:\/\/binaryworld.net\/blogs\/","name":"BinaryWorld Blog","description":"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0","name":"Binary World","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","caption":"Binary World"},"description":"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.","url":"https:\/\/binaryworld.net\/blogs\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/905"}],"collection":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/comments?post=905"}],"version-history":[{"count":0,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/905\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media\/906"}],"wp:attachment":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media?parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/categories?post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/tags?post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}