G
Guest
Is there an easy way to read a fixed length file that will be parsed and it’s
data inserted into a DB table?
Currently, the fixed length file contains 61 columns, and I am defining
variables in my code to store those values. I am also defining the variables
that specifies the size and position of the columns in the file so I can loop
though the columns and rows correctly. Below is a code snippet to give you
an idea of what I am doing. I am open to better way of do this.
public override void FileReader()
{
// read from the file
StreamReader oFs;
string sTemp;
string[] aData;
Hashtable oRow;
string sFile;
bool bAddRec = !CumulativeResults;
string sFileNumber;
string sAccountInfo = string.Empty;
Int32 iCounter = 0;
// define fields, positions, and lengths
string sNumber;
Int32 iNumberLen = 10;
const Int32 coniNumberPos = 0;
string sCustomerNumber;
Int32 iCustomerNumberLen = 30;
const Int32 coniCustomerNumberPos = 1;
string sPhone1;
Int32 iPhone1Len = 10;
const Int32 coniPhone1Pos = 2;
sFile = WorkingPath;
if (File.Exists(sFile))
{
oFs = File.OpenText(sFile);
sTemp = oFs.ReadLine();
while (sTemp != string.Empty)
{
// parse the line
iStartPos = 0;
sNumber = sTemp.Substring(iStartPos, iNumberLen).Trim;
iStartPos += iNumberLen;
sCustomerNumber = sTemp.Substring(iStartPos, iCustomerNumberLen).Trim;
iStartPos += iCustomerNumberLen;
sPhone1 = sTemp.Substring(iStartPos, iPhone1Len).Trim;
iStartPos += iPhone1Len;
// completed parsing the line
oRow = new Hashtable();
if (!Information.IsNumeric(sNumber)) {
sNumber = INVALID_ACCOUNT_ID.ToString;
}
oRow.Add("FileNumber".ToLower, sNumber);
// Account
oRow.Add("Account".ToLower, sCustomerNumber);
// HomePhone
sPhone1 = CleanPhoneNumber(sPhone1);
oRow.Add("HomePhone".ToLower, sPhone1);
// increment our row counter...
iCounter += 1;
base.DataCol.Add(oRow);
}
sTemp = oFs.ReadLine();
}
}
data inserted into a DB table?
Currently, the fixed length file contains 61 columns, and I am defining
variables in my code to store those values. I am also defining the variables
that specifies the size and position of the columns in the file so I can loop
though the columns and rows correctly. Below is a code snippet to give you
an idea of what I am doing. I am open to better way of do this.
public override void FileReader()
{
// read from the file
StreamReader oFs;
string sTemp;
string[] aData;
Hashtable oRow;
string sFile;
bool bAddRec = !CumulativeResults;
string sFileNumber;
string sAccountInfo = string.Empty;
Int32 iCounter = 0;
// define fields, positions, and lengths
string sNumber;
Int32 iNumberLen = 10;
const Int32 coniNumberPos = 0;
string sCustomerNumber;
Int32 iCustomerNumberLen = 30;
const Int32 coniCustomerNumberPos = 1;
string sPhone1;
Int32 iPhone1Len = 10;
const Int32 coniPhone1Pos = 2;
sFile = WorkingPath;
if (File.Exists(sFile))
{
oFs = File.OpenText(sFile);
sTemp = oFs.ReadLine();
while (sTemp != string.Empty)
{
// parse the line
iStartPos = 0;
sNumber = sTemp.Substring(iStartPos, iNumberLen).Trim;
iStartPos += iNumberLen;
sCustomerNumber = sTemp.Substring(iStartPos, iCustomerNumberLen).Trim;
iStartPos += iCustomerNumberLen;
sPhone1 = sTemp.Substring(iStartPos, iPhone1Len).Trim;
iStartPos += iPhone1Len;
// completed parsing the line
oRow = new Hashtable();
if (!Information.IsNumeric(sNumber)) {
sNumber = INVALID_ACCOUNT_ID.ToString;
}
oRow.Add("FileNumber".ToLower, sNumber);
// Account
oRow.Add("Account".ToLower, sCustomerNumber);
// HomePhone
sPhone1 = CleanPhoneNumber(sPhone1);
oRow.Add("HomePhone".ToLower, sPhone1);
// increment our row counter...
iCounter += 1;
base.DataCol.Add(oRow);
}
sTemp = oFs.ReadLine();
}
}