批量复制在添加到数据库时复制每一行

时间:2011-02-22 19:24:27

标签: asp.net vb.net sqlbulkcopy

为什么我的批量副本会复制每一行,因此在我的数据库表中,该行显示两次。

Label1.Visible = True
    Dim tourid As New List(Of String)
    tourid.Add(TextBox1.Text)
    Dim tasktype As New List(Of String)
    Dim tourname1 As New List(Of String)
    Dim tasknamelist As New List(Of String)
    Dim dboxdates As New List(Of String)
    Dim dates As New List(Of String)
    Dim firstdates As New List(Of String)
    Dim agent As New List(Of String)
    Dim desc As New List(Of String)

    Dim checkitem As ListItem
    Dim departuredate As Date
    For Each checkitem In dboxes.Items
        If checkitem.Selected Then
            departuredate = checkitem.Text
            dboxdates.Add(departuredate)


            For Each row As GridViewRow In GridView1.Rows
                ' Selects the text from the TextBox


                Dim checkboxstatus As CheckBox = CType(row.FindControl("tasknamebox"), CheckBox)
                If checkboxstatus.Checked = True Then
                    tasknamelist.Add(checkboxstatus.Text)
                    Dim dates1 As TextBox = CType(row.FindControl("tdays"), TextBox)
                    Dim gracep As TextBox = CType(row.FindControl("tgrace"), TextBox)


                    Dim aftersubtraction As DateTime
                    Dim fromatafter As DateTime
                    aftersubtraction = departuredate.AddDays(-dates1.Text)
                    fromatafter = aftersubtraction.AddDays(-gracep.Text)
                    firstdates.Add(fromatafter.ToString("MM/dd/yyyy"))

                    While fromatafter.DayOfWeek = DayOfWeek.Saturday OrElse fromatafter.DayOfWeek = DayOfWeek.Sunday
                        fromatafter = fromatafter.AddDays(-2)
                    End While
                    dates.Add(fromatafter.ToString("MM/dd/yyyy"))




                    Dim txtdesc2 As TextBox = CType(row.FindControl("txtdesc"), TextBox)
                    desc.Add(txtdesc2.Text)

                    Dim tasktype1 As Label = CType(row.FindControl("tasktype"), Label)
                    Dim agentdlist As DropDownList = CType(row.FindControl("agentdlist"), DropDownList)


                    tasktype.Add(tasktype1.Text)

                    agent.Add(agentdlist.text)

                    Dim tourname As String
                    tourname = tname.Text
                    Dim sChars As String = " "
                    tourname1.Add(tourname.TrimEnd(sChars))

                End If
            Next
        End If

        If tasknamelist.Count > dboxdates.Count Then
            Do
                dboxdates.Add(checkitem.Text)

            Loop Until tasknamelist.Count = dboxdates.Count
        End If



        If tasknamelist.Count > tourid.Count Then
            Do
                tourid.Add(TextBox1.Text)

            Loop Until tasknamelist.Count = tourid.Count
        End If

    Next

    table.clear()


    For i As Integer = 0 To ((dates.Count) - 1)
        Dim row = table.NewRow()
        row("Tour") = tourid(i)
        row("TourName") = tourname1(i)
        row("Task") = tasknamelist(i)
        row("Departure") = dboxdates(i)
        row("Due Date") = dates(i)
        row("Task Type") = tasktype(i)
        row("Agent Name") = agent(i)
        row("Completed") = "NO"
        row("Description") = desc(i)
        row("Orig Due") = firstdates(i)
        table.Rows.Add(row)
    Next

    toptable.Visible = False
    bottom.Visible = True


    GridView2.DataSource = table
    GridView2.DataBind()


    Using bcp As SqlBulkCopy = New SqlBulkCopy(SqlDataSource2.ConnectionString)

        bcp.ColumnMappings.Add(0, 1)
        bcp.ColumnMappings.Add(1, 2)
        bcp.ColumnMappings.Add(2, 3)
        bcp.ColumnMappings.Add(3, 4)
        bcp.ColumnMappings.Add(4, 7)
        bcp.ColumnMappings.Add(5, 5)
        bcp.ColumnMappings.Add(6, 10)
        bcp.ColumnMappings.Add(7, 13)
        bcp.ColumnMappings.Add(8, 6)


        bcp.DestinationTableName = "dbo.stagingtasks"


        bcp.WriteToServer(table)

    End Using

1 个答案:

答案 0 :(得分:0)

你做一个table.Rows.Add(行)和一个新的SqlBulkCopy() 他们似乎是一样的?