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

发布于 2024-10-18 09:51:30 字数 4000 浏览 2 评论 0原文

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

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

Why is my bulk copy duplicating each row, so in my database table the row shows twice.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2024-10-25 09:51:30

您正在执行 table.Rows.Add(row) 和 New SqlBulkCopy()
他们好像是一样的?

Your doing a table.Rows.Add(row) and a New SqlBulkCopy()
They seem to be the same?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文