/// <summary> /// 通用函數(shù),讀文本文件 /// </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();
//指定本次數(shù)據(jù)操作進行事務處理 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++;
}
//執(zhí)行事務 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> /// 處理定長文本文件的函數(shù),將字符串轉換成byte[]數(shù)組 /// </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數(shù)組</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);
}