SqlBulkCopy 不工作,没有错误

ZL1*_*tte 2 c# sql vb.net datatable sqlbulkcopy

我正在从 Excel 文件或 CSV 读取数据。我获取这些数据并创建一个数据表。然后我将该数据表与从原始数据库创建的数据表合并。合并有效,我已经整理了所有数据类型和列名。我有很多链接,但大多数都归结为数据类型和列名/列文本大小写。

没有错误。一切顺利。我试图批量复制的数据表在 VS 表查看器中是正确的。当我签入 SQLExpress 时,没有进行任何更改。我正在使用与项目其余部分相同的连接字符串(行删除、添加、编辑等)。

dt.Merge(dtnew)

    Using destinationConnection As SqlConnection = _
                   New SqlConnection(sConnectionString)
        destinationConnection.Open()

        ' Set up the bulk copy object.  
        ' The column positions in the source data reader  
        ' match the column positions in the destination table,  
        ' so there is no need to map columns. 

        Using bulkCopy As SqlBulkCopy = _
          New SqlBulkCopy(destinationConnection)
            bulkCopy.DestinationTableName = _
            "dbo.TableName"

            Try
                ' Write from the source to the destination.
                bulkCopy.WriteToServer(dt)

            Catch ex As Exception
                Console.WriteLine(ex.Message)

            Finally
                ' Close the SqlDataReader. The SqlBulkCopy 
                ' object is automatically closed at the end 
                ' of the Using block.
            End Try
        End Using

    End Using
End Sub
Run Code Online (Sandbox Code Playgroud)

Sar*_*hra 5

也做列映射..

bulkCopy.ColumnMappings.Add("source column name,"destination column name" )
Run Code Online (Sandbox Code Playgroud)

或者如果您在 dt 和 dbo.Tablename 中有相同的列名,那么您可以使用以下代码

For Each clmn As DataColumn In dt.Columns
     bulkCopy.ColumnMappings.Add(clmn.ColumnName, clmn.ColumnName)
Next
Run Code Online (Sandbox Code Playgroud)