sqlbulkcopy - 不允许DBNull.Value。?

时间:2012-08-31 16:14:17

标签: vb.net sqlbulkcopy

我正在尝试下面的这两个调用但是它们都返回一个异常Customer_ID不允许DBNull.Value。但是当我调试时,我看到我的所有记录都分配了Customer_ID。它是我的数据库表中定义的唯一一个不可为空的。是什么导致了这个错误?

   bulkCopy.WriteToServer(myBookingDataTable)

   bulkCopy.WriteToServer(myBookingss.ToArray())

这是我的整个代码。

    Using myConnection As SqlConnection = _
                New SqlConnection(connectionString)
                myConnection.Open()


                Using bulkCopy As SqlBulkCopy = _
                  New SqlBulkCopy(My.Settings.ConnectionString(), SqlBulkCopyOptions.Default)

                    bulkCopy.DestinationTableName = "dbo.Booking"

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

                        'bulkCopy.WriteToServer(myBookingss.ToArray())

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

                    Finally

                        bulkCopy.Close()
                    End Try
                End Using

1 个答案:

答案 0 :(得分:14)

确保源和目标中字段的顺序相同。