批量复制每行插入两次

时间:2011-02-24 17:14:40

标签: asp.net vb.net stored-procedures

现在我让它工作但是批量复制是将行插入两次,而不是一次

并且我知道该表具有正确的行,因为我可以在gridview中正确地看到它

我修复了这一部分
当我在调试模式下运行它时,我的程序运行正常,但是当我将它放在服务器上时,它没有运行存储过程,当我把它放在我的服务器上时。

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 ((tasknamelist.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
            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

        cmd1.CommandText = "dbo.taskadding"
        cmd1.CommandType = CommandType.StoredProcedure
        cmd1.Connection = conn

        conn.Open()
        cmd1.BeginExecuteNonQuery()
        conn.Close()

2 个答案:

答案 0 :(得分:1)

您是否为两者使用相同的数据库服务器? (也就是说,你的连接字符串实际上是硬编码的,还是为了简单起见而修改它?)

如果服务器未启用混合模式身份验证,您将无法使用用户名和密码进行连接。

<强>更新

正如@santiagoIT所提到的,如果你对此进行异常处理并记录正在生成的异常,你很可能会很快看到出现了什么问题。

修改

根据评论中的请求,以下是如何使用(基本)日志记录实现异常处理:

Try
    'Your block of code
Catch ex As Exception
    System.IO.File.WriteAllText("MyLogFile.log", ex.ToString())
End Try

正如@santiagoIT所提到的,存在各种日志框架以使日志记录更加健壮和稳定(例如,如果日志文件已经在其他地方打开,我所做的就会抛出它自己的例外)。我建议使用这样的方法来获得真正的解决方案,但我所展示的内容将帮助您调试手头的问题。

答案 1 :(得分:1)

我建议您安装ELMAH。添加它是微不足道的,但会给你巨大的好处。 这是一个错误记录框架。 安装完成后,在ELMAH错误日志中查找并查看异常详细信息。它肯定与数据库相关,错误日志将为您提供足够的信息以了解问题所在。