BulkCopy错误“`数据源中String类型的给定值无法转换为指定目标列的int类型。

时间:2014-04-09 05:12:13

标签: .net vb.net sqlbulkcopy

我正在尝试使用BulkUpload将数据从CSV导入DataBase,我能够复制varchar列,但是当我尝试导入整数列时无法导入。 错误:The given value of type String from the data source cannot be converted to type int of the specified target column.

更新

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dt As New DataTable()
        Dim line As String = Nothing
        Dim i As Integer = 0

        Using sr As StreamReader = File.OpenText(txtfileName.Text)
            line = sr.ReadLine()
            Do While line IsNot Nothing
                Dim data() As String = line.Split(","c)
                If data.Length > 0 Then
                    If i = 0 Then
                        If i > 4 Then
                            Dim column As DataColumn = New DataColumn
                            If i = 6 Then
                                column.DataType = System.Type.GetType("System.Decimal")
                                column.AllowDBNull = False
                                column.Caption = "Price"
                                column.ColumnName = "Price"
                                column.DefaultValue = 0
                            End If
                            dt.Columns.Add(column)
                        Else
                            For Each item In data
                                dt.Columns.Add(New DataColumn())

                            Next item
                            i += 1
                        End If
                    End If
                    Dim row As DataRow = dt.NewRow()
                    row.ItemArray = data
                    dt.Rows.Add(row)
                End If
                line = sr.ReadLine()
            Loop
        End Using


        Using cn As New SqlConnection("Data Source=xx.xxx.in;Initial catalog=xxx;User Id=xxx;Password=xx@xx;")
            cn.Open()
            Using copy As New SqlBulkCopy(cn)
                copy.ColumnMappings.Add(0, 0)
                copy.ColumnMappings.Add(1, 1)
                copy.ColumnMappings.Add(2, 2)
                copy.ColumnMappings.Add(3, 3)
                copy.ColumnMappings.Add(4, 4)
                copy.ColumnMappings.Add(5, 5)
                copy.ColumnMappings.Add(6, 6)

                copy.DestinationTableName = "tbl_Bonds"
                'dt.Columns(5).DataType = GetType(Decimal)
                'dt.Columns(6).DataType = GetType(Decimal)
                copy.WriteToServer(dt)
            End Using
        End Using

    End Sub

1 个答案:

答案 0 :(得分:1)

您的DataColumns是默认列。您永远不会设置名称或类型。

查看MSDN中有关如何创建与数据库列匹配的DataColumn的示例。至少类型应该是相同的。

在粘贴示例之前,您需要先了解语言和问题。您需要将要插入的列设置为数据表,而不是之后未使用的随机列:

Dim column As DataColumn = New DataColumn
  If i = 6 Then
    column.DataType = System.Type.GetType("System.Decimal")
    column.AllowDBNull = False
    column.Caption = "Price"
    column.ColumnName = "Price"
    column.DefaultValue = 0
  End If
dt.Columns.Add(column)

为什么你要在循环中这样做呢?在读取数据之前这样做。