Click here to Skip to main content
15,892,737 members
Articles / Programming Languages / C#

Wrapper Class for Parsing Fixed-Width, Multiple Section Files

Rate me:
Please Sign up or sign in to vote.
4.07/5 (4 votes)
21 Apr 2006CPOL8 min read 51.6K   1.1K   33  
An article describing a wrapper class to import very large multiple section reports, typically from a legacy system, into the modern SQL Server or other RDBMS.
using System;
using System.Collections;
using System.IO;
using System.Configuration ;

namespace ReportParserDemo 
{
	/// <summary>
	/// Summary description for DistrictTrafficMailerImporter.
	/// </summary>
	class Application: System.ComponentModel.Component 
	{
		private System.Diagnostics.EventLog PrintImportLog;
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			new Application().Run();
		}
		public Application()
		{
		}

		public void Run()
		{
			InitializeComponent();
			bool quit = false;
			while (!quit)
			{
				try
				{
					quit = DisplayUserMenu();
				}
				catch (Exception x)
				{
					Console.WriteLine("Error: {0}", x.Message);
				}                       
			}
		}
	
		private char UserChoice()
		{                 
			Console.WriteLine("(I)mport File\t(Q)uit");
			string choice = Console.ReadLine();
			return Char.ToLower(choice[0]);
		}

		private bool DisplayUserMenu()
		{
			switch (UserChoice())
			{
				case 'i':
				{
					RunImport();
					break;
				}
				case 'q':
				{
					return true;
				}
			}
			return false;
		}

		private void InitializeComponent()
		{
			this.PrintImportLog = new System.Diagnostics.EventLog();
			((System.ComponentModel.ISupportInitialize)(this.PrintImportLog)).BeginInit();
			// 
			// PrintImportLog
			// 
			this.PrintImportLog.Log = "Application";
			this.PrintImportLog.Source = "Mainframe File Import Monitor";
			((System.ComponentModel.ISupportInitialize)(this.PrintImportLog)).EndInit();

		}

		private void RunImport()
		{
			//string ConnectionString = @"data source=(local);initial catalog=Databasename;uid=userid;pwd=password"; //ConfigurationSettings.AppSettings["ConnectionString"];  
			string ImportFile = @"E:\Development\Projects\ReportParserDemo01\ReportParserDemo\printimport.txt"; // ConfigurationSettings.AppSettings["ImportFile"]; 	
			FileInfo fi = new FileInfo(ImportFile);
			Importer importer = new Importer();
			importer.FileName = fi.Name;
			importer.FullName = fi.FullName;
			importer.Log = PrintImportLog;		
			bool Status = importer.Execute();
			if (Status == true) 
				Console.WriteLine(String.Format("Imported {0} Successfully.", ImportFile));
			else
				Console.WriteLine(String.Format("Import of file {0} Failed!", ImportFile));
		}



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Tampa, FL developer with about 11 years of experience.

Comments and Discussions