BULK INSERT时发生错误 异常的EOF,原因是有空行!

BULK INSERT时发生错误 异常的EOF,原因是有空行!

或者文本结束时没0x0D0A。

//特别写一个过程来进行处理,找出这些问题。

long ll_rows,i,ll_read_len,ll_linecount,j
string ls_path

long ll_filehandle
blob{10485760} bl
byte bt[10485760]

ll_rows = dw_2.rowcount()

if ll_rows <1 then return
for i = 1 to ll_rows
yield()

if ib_stop then return

dw_2.selectrow(0,false)
dw_2.selectrow(i,true)

ls_path = dw_2.getitemstring(i,6) //filepath
ll_filehandle = FileOpen(ls_path, &
StreamMode!, read!,shared!, append!)

if ll_filehandle < 1 then continue

//先判断文件尾是否是0x0D0A
FileSeek64 (ll_filehandle,-2,FromEnd!)

filereadex(ll_filehandle,bl,2)

if byte(blobmid(bl,1,1)) <> 13 or byte(blobmid(bl,2,1)) <> 10 then
dw_2.setitem(i,2,dw_2.getitemstring(i,2) + "---结尾不为\r\n")
//messagebox("异常","文件:~n" + ls_path + "~n结尾不为\r\n")
fileclose(ll_filehandle)
continue
end if

//判断文件中间是否存在连续的0D0A0D0A这样的空行(跳行),其实严格地说,还有其他不可打印字符(控制字符是否跳行没测试)也可能引起。我遇到的问题就是别人导给我的数据,有一列的汉字被切掉了一半!搞死人了。我叫他先把那一栏加个因为字符,再切掉一个字符(等于切掉半个汉字了),他有不肯定搞,还是他根本不晓得导出的结果会那样。再说几百兆的文件他也不可能逐行去看。所以其他跳行原因请具体分析。


FileSeek64 (ll_filehandle,1,FromBeginning!)


DO while(true)
ll_read_len = filereadex(ll_filehandle,bl,10485760)
bt = GetByteArray(bl)//这里很关键,用blobmid去逐个取字符很慢。
if ll_read_len <4 then exit

ll_read_len -=3

for j = 1 to ll_read_len
yield()

if ib_stop then
fileclose(ll_filehandle)
return
end if

if mod(j,100000) = 0 then
st_odoa.text = "0x0D0A:" + string(ll_linecount)
st_ij.text = "J: " + string(j)
end if

if bt[j]<>13 then continue

if bt[j + 1] = 10 then
ll_linecount ++
else
continue
end if

if bt[j + 2] = 13 and bt[j + 3] = 10 then
dw_2.setitem(i,2,dw_2.getitemstring(i,2) + "---存在空行,在" + string(ll_linecount) + "行处!")
//messagebox("异常","文件:~n" + ls_path +&
//"~n在中间存在空行~n位置: 第:" + string(ll_rowcount) +&
//"行处!",question!,yesno!)
exit
end if
next

FileSeek64 (ll_filehandle,-3,FromCurrent!) //回退
loop

fileclose(ll_filehandle)
next
messagebox("提示","文件检查结束")

代码分析: private void RMImport(DataTable dt) { this.Cursor = Cursors.WaitCursor; string histmon = Convert.ToString(dt.Rows[1][1]).Replace("-", "").Substring(0, 6); #region Stocked //删除所传数据月份数据 string sql_del = @" DELETE FROM icost_detail_obms_rm_stock WHERE histmonth = '" + histmon + @"' "; DataTool.ExecuteSql(sql_del); DataTable dt_stock = new DataTable(); dt_stock = dt.Clone(); foreach (DataRow dr in dt.Rows) { if (Convert.ToString(dr[0]).Contains("Non-stocked")) { break; } dt_stock.ImportRow(dr); } // 确保至少有6行 if (dt_stock.Rows.Count >= 6) { // 获取列名--第六行的值 DataRow row6 = dt_stock.Rows[5]; for (int i = 0; i < dt_stock.Columns.Count; i++) { dt_stock.Columns[i].ColumnName = row6[i].ToString().Replace("/", "").Replace(" ", "").Replace(".", "").Trim().ToUpper(); } // 删除前五行(多余行) for (int i = 0; i < 6; i++) { dt_stock.Rows.RemoveAt(0); } } //检查是否有空行 DataRow[] delrows = dt_stock.Select("costcenter = '' OR costcenter IS NULL"); foreach (DataRow row in delrows) { dt_stock.Rows.Remove(row); } dt_stock.Columns.Add("GRADE1"); dt_stock.Columns.Add("HISTMONTH"); foreach (DataRow dr in dt_stock.Rows) { dr["GRADE1"] = "Fab1"; dr["HISTMONTH"] = histmon; } int j = 0; if (dt_stock != null) { j = DataTool.BulkInsert("icost_detail_obms_rm_stock", dt_stock); } #endregion #region Non-Stocked //删除所传数据月份数据 string sql_del1 = @" DELETE FROM icost_detail_obms_rm_non_stock WHERE histmonth = '" + histmon + @"' "; DataTool.ExecuteSql(sql_del1); dt.Columns[0].ColumnName = "fab"; DataTable dt_nonstock = new DataTable(); dt_nonstock = dt.Copy(); dt_nonstock.DefaultView.RowFilter = "fab in('Plant','PT01')";//plant为后续要获取的列名行;PT01固定不变 dt_nonstock = dt_nonstock.DefaultView.ToTable(); // 确保至少有1行 if (dt_nonstock.Rows.Count >= 1) { List<string> emptycol = new List<string>(); // 获取列名--第一行的值 DataRow row1 = dt_nonstock.Rows[0]; for (int i = 0; i < dt_nonstock.Columns.Count; i++) { if (!string.IsNullOrEmpty(Convert.ToString(row1[i]))) { if (Convert.ToString(row1[i]).ToUpper() == "USER")//需特殊处理的列名 与数据库中保持一致 { dt_nonstock.Columns[i].ColumnName = "USERID"; } else { dt_nonstock.Columns[i].ColumnName = row1[i].ToString().Replace("/", "").Replace(" ", "").Replace(".", "").Trim().ToUpper(); } } else { emptycol.Add(dt_nonstock.Columns[i].Caption); } } // 删除多余列 否则影响BulkInsert for (int i = 0; i < emptycol.Count; i++) { dt_nonstock.Columns.Remove(emptycol[i]); } // 删除第一行(多余行) for (int i = 0; i < 1; i++) { dt_nonstock.Rows.RemoveAt(0); } } //检查是否有空行 DataRow[] delrows1 = dt_nonstock.Select("plant = '' OR plant IS NULL"); foreach (DataRow row in delrows1) { dt_nonstock.Rows.Remove(row); } dt_nonstock.Columns.Add("GRADE1"); dt_nonstock.Columns.Add("HISTMONTH"); foreach (DataRow dr in dt_nonstock.Rows) { dr["GRADE1"] = "Fab1"; dr["HISTMONTH"] = histmon; } int y = 0; if (dt_nonstock != null) { y = DataTool.BulkInsert("icost_detail_obms_rm_non_stock", dt_nonstock); } #endregion if (j > 0 && y > 0) { DevExpress.XtraEditors.XtraMessageBox.Show("RM相关数据保存成功"); } else { if (j > 0 && y == 0) { DevExpress.XtraEditors.XtraMessageBox.Show("Non-Stocked部分保存失败!请联系开发人员"); } else if (j == 0 && y > 0) { DevExpress.XtraEditors.XtraMessageBox.Show("Stocked部分保存失败!请联系开发人员"); } else { DevExpress.XtraEditors.XtraMessageBox.Show("RM相关数据保存失败!请联系开发人员"); } } this.Cursor = Cursors.Default; }
最新发布
08-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值