gpt4 book ai didi

c# - SqlBulkCopy 不工作,没有错误

转载 作者:行者123 更新时间:2023-11-30 20:51:33 24 4
gpt4 key购买 nike

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

没有错误。一切顺利。我尝试批量复制的数据表在 VS 表查看器中是正确的。当我 checkin 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

关于c# - SqlBulkCopy 不工作,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21555083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com