Dim TaskName As String = args(0)
Dim Filecsv As String = args(1)
Dim TableNam As String = args(2)
Dim HeadRow As Integer
Dim BootomRow As Integer
Dim cmd As SQLCommand
Dim DtYueShu As DataTable
Dim SaveFlag As Integer = 0
Dim dradd As Row
If cstr(args(3)) = "" Then
HeadRow = 1 '标题头占几行,为空默认为1
Else If cint(args(3)) < 0 Then
messagebox.show("行参数错误","注意")
Return 1
Else
HeadRow = args(3)
End If
If cstr(args(4)) = "" Then
BootomRow = 0 '底部汇总占几行,为空默认为0
Else If cint(args(4)) < 0 Then
messagebox.show("底部行参数错误","注意")
Return 1
Else
BootomRow = args(4)
End If
Dim ColNum As Integer
Dim YsFiler As String
Dim zd As New Dictionary(Of String, Integer)
Dim zdfan As New Dictionary(Of Integer, String)
Dim drs As List(of DataRow)
Dim path As String
Dim tempstr() As String = Filecsv.split("\")
path = left(filecsv,len(filecsv) - len(tempstr(tempstr.length - 1)))
'获取内外部对应数据及约束列
cmd = New SQLCommand
cmd.CommandText = "select * Fro m {importrelation} where 1 = 2"
cmd.Cfont-family: Verdana;">DtYueShu = cmd.ExecuteReader()
'messagebox.show(2)
'Dim YingShe As List(of DataRow) = DataTables("importrelation").sqlselect("表名称 = '" & TableNam & "'")
Dim YingShe As List(of DataRow) = DtYueShu.sqlselect("表名称 = '" & TableNam & "'")
Dim YsCount As Integer = YingShe.count
'messagebox.show(1)
If YingShe Is Nothing Then
FileSys.WriteAllText(path & "log.txt", Date.now & ": " & filecsv & "表格式未设置映射,请及时查看" & chr(10), True, Encoding.Default)
Return 1
End If
'第一次需加载表
If taskctrdic.containskey(taskname) Then
If TaskCtrDic(taskname).Tableisconfig = False Then
'messagebox.show(2)
'设置字段类型
For i As Integer = 0 To YsCount - 1
If YingShe(i)("数据类型") = "DateTime" Then
DataTables(TableNam).DataCols(YingShe(i)("外部列字段")).SetDateTimeFormat(DateTimeFormatEnum.DateLongTime)
End If
Next
TaskCtrDic(taskname).Tableisconfig = True
End If
End If
'messagebox.show("aaa")
'读取数据然后分行
Dim str As String = FileSys.ReadAllText(Filecsv, Encoding.Default)
'符号处理
str = str.replace("""", "").trim()
str = str.replace("null", "0").trim()
str = str.replace("'", "").trim()
'分行处理
Dim ary() As String = str.Split(chr(10))
'获取约束列信息
Dim YueShu As List(of DataRow) = DtYueShu.sqlselect("表名称 = '" & TableNam & "' And 是否约束列 = True")
'Dim YueShu As List(of DataRow) = DataTables("importrelation").sqlselect("表名称 = '" & TableNam & "' And 是否约束列 = True")
Dim YssCount As Integer = YueShu.count
Dim DataStartRow As Integer
If headrow <> 0 Then
DataStartRow = headrow - 1
Else
DataStartRow = headrow
End If
'messagebox.show(3)
'分行处理
For Rowi As Integer = 0 To ary.Length - 1 - BootomRow
'yanshi(1)
Application.Doevents
'taskctrdic(taskname).ThreadOpe.sleep(10)
If Rowi Mod 50 = 0 Then
If taskctrdic.containskey(taskname) = False Then
'taskctrdic.remove(taskname)
Return 1
End If
End If
'messagebox.show(ary.length - 1 - BootomRow)
'Return 1
'第一列为列标头,需要判断是否和存储表一致
Dim ColumnStr() As String = ary(Rowi).Split(",")
ColNum = Columnstr.length
If Rowi <> DataStartRow Then
If vars("ColNum") <> ColNum - 1 Then
FileSys.WriteAllText(path & "log.txt", Date.now & ": " & filecsv & "第" & (Rowi + 1) & "行,表格式有问题,请及时查看" & chr(10), True, Encoding.Default)
Return 1
End If
End If
'messagebox.show(Colnum)
'默认第一行未标题,未对无列标题的异常进行判断,暂未发现由此场景
If Rowi = DataStartRow Then
vars("ColNum") = ColNum - 1
For m As Integer = 0 To ColNum - 1
Application.Doevents
Dim Drtmp As DataRow
Drtmp = DtYueShu.sqlfind("内部列字段 = '" & ColumnStr(m).trim(" ") & "'" & " And 表名称 = '" & TableNam & "'")
If Drtmp IsNot Nothing Then
zd.Add(ColumnStr(m).trim(" "),m)
zdfan.add(m,drtmp("外部列字段"))
'messagebox.show(ColumnStr(m))
Else
FileSys.WriteAllText(path & "log.txt", Date.now & ": " & filecsv & "表格式有问题,请及时查看" & chr(10), True, Encoding.Default)
Return 1
End If
Next
If ColNum > YsCount Then
messagebox.show("导入表的列数大于存储表列数,请检查","注意")
Return 1
Else '列头编号
End If
Continue For
End If
'数据导入
YsFiler = ""
For n As Integer = 0 To YssCount - 1
If n = YssCount - 1 Then
YsFiler = YsFiler & YueShu(n)("外部列字段") & " = '" & ColumnStr(zd(YueShu(n)("内部列字段"))).trim(""""," ","=") & "'"
Else
YsFiler = YsFiler & YueShu(n)("外部列字段") & " = '" & ColumnStr(zd(YueShu(n)("内部列字段"))).trim(""""," ","=") & "' And "
End If
Application.Doevents
Next
drs = DataTables(TableNam).SQLSelect(YsFiler)
If drs.count = 0 Then
dradd = Tables(TableNam).AddNew()
For j As Integer = 0 To ColNum - 1
'messagebox.show(j)
'messagebox.show(YingShe(j)("内部列字段"))
'messagebox.show(YingShe(j)("外部列字段"))
'messagebox.show(zd(YingShe(j)("内部列字段")))
'messagebox.show("OK")
'dradd(YingShe(j)("外部列字段")) = ColumnStr(zd(YingShe(j)("内部列字段"))).trim(""""," ","=","元")
dradd(zdfan(j)) = ColumnStr(j)
Application.Doevents
Next
While dradd.DataRow.RowState <> DataRowState.Unchanged
'If dradd.DataRow.RowState <> DataRowState.Unchanged Then
dradd.save()
Application.Doevents
End While
Else
'更新数据暂未考虑
Application.Doevents
End If
'SaveFlag = SaveFlag + 1
'If SaveFlag Mod 50 = 0 Then
' DataTables(TableNam).save()
'End If
Next
'DataTables(TableNam).save()
If taskctrdic.containskey(taskname) Then
TaskCtrDic(Taskname).CurrentNum = TaskCtrDic(Taskname).CurrentNum + 1
End If
Return 0
[此贴子已经被作者于2015/10/11 19:51:01编辑过]