Как читать REQ файлы без столбца заголовка в C#
У меня есть этот код консольного приложения c# для чтения файлов REQ из папки, и он работает со столбцом заголовка ,но мой файл REQ без столбца заголовка это, например, то, что я имею в виду :
123123*JAN*100001*ssssss_jan@gmail.com*CREDITADVICE*SDG*1000000*20170620*F0000999*10.105.1.20
это код:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data.SqlClient; using System.Data; namespace readREQ { class Program { static void Main(string[] args) { string datetime = DateTime.Now.ToString("yyyyMMddHHmmss"); string LogFolder = @"C:\Log\"; try { //Declare Variables and provide values string SourceFolderPath = @"C:\Source\"; //Provide the Source Folder where files are present string FileExtension = ".txt"; //Provide the extension of files you need to load, can be .txt or .csv string FileDelimiter = ","; // provide the file delimiter such as comma or pipe string ArchiveFolder = @"C:\Archive\"; //Provide the archive folder path where files will be moved string TableName = "dbo.Customer"; //Provide the table name in which you would like to load the files. //Create Connection to SQL Server in which you like to load files SqlConnection SQLConnection = new SqlConnection(); SQLConnection.ConnectionString = "Data Source = (local); Initial Catalog =TechBrothersIT; " + "Integrated Security=true;"; //Reading file names one by one string[] fileEntries = Directory.GetFiles(SourceFolderPath, "*" + FileExtension); foreach (string fileName in fileEntries) { //Writing Data of File Into Table int counter = 0; string line; string ColumnList = ""; System.IO.StreamReader SourceFile = new System.IO.StreamReader(fileName); SQLConnection.Open(); while ((line = SourceFile.ReadLine()) != null) { if (counter == 0) { //By using Header Row, Build Column List ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]"; } else { //Build and Execute Insert Statement to insert record string query = "Insert into " + TableName + " (" + ColumnList + ") "; query += "VALUES('" + line.Replace(FileDelimiter, "','") + "')"; SqlCommand SQLCmd = new SqlCommand(query, SQLConnection); SQLCmd.ExecuteNonQuery(); } counter++; } SourceFile.Close(); SQLConnection.Close(); //move the file to archive folder after adding datetime to it File.Move(fileName, ArchiveFolder + "\\" + (fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "") + "_" + datetime + FileExtension); } } catch (Exception exception) { // Create Log File for Errors using (StreamWriter sw = File.CreateText(LogFolder + "\\" + "ErrorLog_" + datetime + ".log")) { sw.WriteLine(exception.ToString()); } } } } }
Что я уже пробовал:
У меня есть попытка удалить эти строки
if (counter == 0) { //By using Header Row, Build Column List ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]"; }
но все же есть ошибка