using System;
using System.IO;
using System.Data;
using System.Configuration;
namespace Text_Import
{
public class TextAnalyzer
{
public TextAnalyzer()
{
}
// Get The File Path to the ASCII database
public string FilePath()
{
string filePath;
filePath = ConfigurationSettings.AppSettings["path"];
return(filePath);
}
// Find the number of rows in the file
public int RowAmount()
{
int rowAmount;
StreamReader cRows = new StreamReader(@"C:\test.txt");
string cLine = cRows.ReadLine();
int charAmount = Int32.Parse(cLine.Length.ToString());
string cAll = cRows.ReadToEnd();
int allAmount = Int32.Parse(cAll.Length.ToString());
rowAmount = (allAmount)/(charAmount);
return(rowAmount);
}
// Read Each Row
int RowNum;
int Position;
public string ReadRow()
{
string readRow;
StreamReader rRow = new StreamReader(@"C:\test.txt");
string OneRow = rRow.ReadLine();
string AllText = rRow.ReadToEnd();
int RowLength = Int32.Parse(OneRow.Length.ToString()) + 2;
int EntireLength = Int32.Parse(AllText.Length.ToString());
if (Position <= EntireLength - 1830)
{
Position = (RowLength * RowNum);
RowNum++;
}
readRow = AllText.Substring(Position, RowLength);
return(readRow);
}
// The data Table!!!
public DataTable Table()
{
TextAnalyzer ta = new TextAnalyzer();
ta.FilePath();
DataTable Table = new DataTable("Main Table");
DataRow row;
DataColumn ISBN = new DataColumn("ISBN");
DataColumn IDC = new DataColumn("IDC");
DataColumn UPC = new DataColumn("UPC");
DataColumn Title = new DataColumn("Title");
DataColumn SeriesID = new DataColumn("sID");
DataColumn ID = new DataColumn("ID");
ID.AutoIncrement = true;
Table.Columns.Add(ISBN);
Table.Columns.Add(IDC);
Table.Columns.Add(UPC);
Table.Columns.Add(Title);
Table.Columns.Add(SeriesID);
Table.Columns.Add(ID);
for (int i=1;i<=ta.RowAmount();i++)
{
row = Table.NewRow();
string rowParse = ta.ReadRow();
row[ISBN] = rowParse.Substring(0, 10);
row[IDC] = rowParse.Substring(11, 1);
row[UPC] = rowParse.Substring(442, 17);
row[Title] = rowParse.Substring(11, 150);
row[SeriesID] = rowParse.Substring(212, 9);
Table.Rows.Add(row);
}
return(Table);
}
}
}