/// <summary>
/// 通用函数,读文本文件
/// </summary>
/// <param name="fileName">读入的文本文件名称</param>
public static void ReadTextFromFileName(
string fileName)

{
string strRecord = "";
//读入文本文件时,一定要指定文件的编码格式.其中:default为文本文件本来的编码格式
//如果是简体中文的文本文件,也可以这样设置编码格式: System.Text.Encoding.GetEncode("gb2312")
//Encoding.GetEncode("gb2312")为简体中文编码格式,Encoding.GetEncode("big5")为繁体中文编码格式. 
StreamReader reader =
new StreamReader(fileName,System.Text.Encoding.Default);

da =
new DataAccess();

da.OpenConnection();
//指定本次数据操作进行事务处理 
da.StartTrans =
true;
//开始事务处理 
da.BeginTrans();
//i is the really row
//j is the row of writed to database
int i ,j;

i=0;

j=0;
try 
{
while (reader.Peek() >= 0)

{

strRecord = reader.ReadLine();
if (StringConvertByteArray(strRecord))

{

j++;

}

i++;

}
//执行事务 
da.Commit();

TotalLine = i;

RealLine = j;

}
catch (Exception ex)

{
//事务回滚 
da.Rollback();

SystemError.SystemLog(
"文件:" + fileName +
"导入失败,错误行是第"+ i.ToString()+
"行,原因是: " + ex.Message);
throw new Exception(ex.Message);

}
//相关资源的消除
finally 
{

reader.Close();

da.CloseConnection();

}

}
/// <summary>
/// 处理定长文本文件的函数,将字符串转换成byte[]数组
/// </summary>
/// <param name="aRecord"></param>
private static bool StringConvertByteArray(
string aRecord)

{
//解决文本文件一行中可能存在中文的情况,将string类型转换为byte[]来达到
//正确处理文本文件的目的
byte[] repRecord = System.Text.Encoding.Default.GetBytes(aRecord);
//判断取得的文本文件长度是否等于定义的文本文件长度
if (repRecord.Length != iLineLength)

{

SystemError.SystemLog(
"文件:" + fileName +
"导入出错,出错原因是文件长度不符合");
throw new Exception(
"文件文本长度不对,导入失败,请检查文件文件格式");

}
bool isInsert=
false;

isInsert = AddRecord(

GetString(repRecord,0,8),

GetString(repRecord,8,8),

GetString(repRecord,16,6),

GetString(repRecord,22,6),

GetString(repRecord,28,8),

GetString(repRecord,36,6),

GetString(repRecord,42,10),

GetString(repRecord,52,4),

GetString(repRecord,56,6),

GetString(repRecord,62,8),

GetString(repRecord,70,7),

GetString(repRecord,77,32),

GetString(repRecord,109,72),

GetString(repRecord,181,8),

GetString(repRecord,189,30),

GetString(repRecord,219,45),

GetString(repRecord,264,10),

GetString(repRecord,274,25),

GetString(repRecord,299,2),

GetString(repRecord,301,25),

GetString(repRecord,326,3),

GetString(repRecord,329,15),

GetString(repRecord,344,1),

GetString(repRecord,345,8),

GetString(repRecord,353,6),

GetString(repRecord,359,8),

GetString(repRecord,367,1),

GetString(repRecord,368,1),

GetString(repRecord,369,32),

GetString(repRecord,401,7),

GetString(repRecord,408,60),

GetString(repRecord,468,20),

GetString(repRecord,488,20),

GetString(repRecord,508,20),

GetString(repRecord,528,36),

GetString(repRecord,564,15),

GetString(repRecord,579,15),

GetString(repRecord,594,15)

);
return isInsert;

}
//private static void
/// <summary>
/// 处理长度固定的文本文,读取到每个字段的值
/// </summary>
/// <param name="aStr">文本文件的每行文本转换的Byte数组</param>
/// <param name="iStart">读取的起始位置</param>
/// <param name="iLength">读取的长度</param>
/// <returns>返回的字符串,对应于具体的字段值</returns>
private static string GetString(
byte[] aStr,
int iStart,
int iLength)

{
byte[] tempStr =
new byte[iLength];
for (
int i = 0; i < iLength; i ++)

{

tempStr[i] = (
byte)aStr.GetValue(iStart + i);

}
return System.Text.Encoding.Default.GetString(tempStr);

}