您的位置:首页 > 编程语言 > VB

vb.net导入excel,生成上级parent_id

2012-08-09 12:23 459 查看
tb_name = "tb_cat_department"

Dim depart_name As String = Nothing

Dim post_type As String = Nothing

Dim parent_id As Integer = 0

Dim tmp_id(1) As Integer

i = 0

For Each tmpdataRow In tmpDatatableX.Rows

i = i + 1

If i > 1 Then '导入数据从第2行开始

post_type = Convert.ToString(tmpdataRow(3))

For j = 0 To cols - 2

If Len(Convert.ToString(tmpdataRow(j))) > 0 Then

Dim rand = New System.Random()

Dim randx As String = rand.Next(100, 999).ToString() '随机数

depart_name = Convert.ToString(tmpdataRow(j))

Select Case j

Case 0 '第一列:部门

post_type = ""

parent_id = 0

Case 1 '第二列:岗位1

If Len(Convert.ToString(tmpdataRow(2))) > 0 Then

post_type = ""

End If

parent_id = tmp_id(0)

Case 2 '第二列:岗位2

parent_id = tmp_id(1)

Case Else

End Select

Sql_Str = "insert into [" + tb_name + "] " _

& "(depart_name,post_type,parent_id,import_date) " _

& " values ('" & depart_name & "','" & post_type & "'," & parent_id & ",'" & import_date + randx & "')"

TrueorFalse = ExecuteSqlstr(Sql_Str)

If TrueorFalse = True Then

wite_log("数据导入成功:" & depart_name & "','" & post_type & "'," & parent_id & ",'" & import_date + randx)

Sql_Str = "select id from [" + tb_name + "] where import_date='" & import_date + randx & "'"

If j = 0 Then

tmp_id(0) = getvaluestring(Sql_Str, "id")

End If

If j = 1 Then

tmp_id(1) = getvaluestring(Sql_Str, "id")

End If

Else

wite_log("数据导入失败:" & depart_name & "','" & post_type & "'," & parent_id & ",'" & import_date + randx)

End If

End If

Next

End If

Next
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: