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

也进行列映射..

bulkCopy.ColumnMappings.Add("source column name,"destination column name" )

,或者,如果您在DT和DBO.TableName中具有相同的列名,则可以使用以下代码

For Each clmn As DataColumn In dt.Columns
     bulkCopy.ColumnMappings.Add(clmn.ColumnName, clmn.ColumnName)
Next

最新更新