【问题标题】:BulkCopy Error" `The given value of type String from the data source cannot be converted to type int of the specified target column.`BulkCopy 错误" `来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。`
【发布时间】:2014-04-09 05:12:13
【问题描述】:

我正在尝试使用 BulkUpload 将数据从 CSV 导入数据库,我能够复制 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

【问题讨论】:

  • 第6列的数据类型是System.Decimal还是整数?试试System.Int32。
  • 我也试过了..
  • 请从您的循环中删除整个数据表构建。这是错误的,并且每次迭代代码都会使情况变得更糟。建立你的数据表。一次。循环之前。废弃无用的i 变量。

标签: .net vb.net sqlbulkcopy


【解决方案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)

你为什么要在循环中这样做?在读取数据之前执行此操作。

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多