1

我是更新数据库的新手,显然我不擅长。

细节 :

  • 我使用 tableadapter(我不知道这是否是最好的方法)在 .mdf 文件中引用我的数据库
  • 我的表有两列,第一列是Primary Key
  • 该表已经有一些行
  • 我用以下内容填充我的表格适配器:Me.CCListTableAdapter.Fill(Me.HQSdbDataSet.CCList)

我正在尝试做的事情:

  1. 我根据我的 CCListTable 创建了一个新的数据表:Dim ccDataTable As CCListDataTable = New CCListDataTable
  2. 我使用 CsvReader 将具有新行(> 3000 行)的 .csv 文件导入到这个新数据表中。我的数据库中已经存在一些行
  3. 然后我尝试将这些新行添加到 tableadapter 中:

    Me.CCListBindingSource.EndEdit() Me.CCListTableAdapter.Update(ccDataTable)

但它不起作用,我得到一个SqlExceptionwith Violation of PRIMARY KEY [...] Cannot insert duplicate key。好的,就像我说的那样,我知道有重复的键,但我不想插入这些键,而只想用新行更新 tableadapter。

这是我创建的用于导入我的 CSV 的子程序(如果有帮助的话):

Public Sub ImportCSV2Data(ByVal filename As String, ByRef datatable As DataTable, ByVal column2Import() As Integer,
                          ByVal tableAlreadyExist As Boolean)

    Dim csvCopy As CachedCsvReader = New CachedCsvReader(New StreamReader(filename), True, ";"c)
    csvCopy.MissingFieldAction = MissingFieldAction.ReplaceByEmpty

    Dim headers() As String = csvCopy.GetFieldHeaders
    If column2Import.Length > 0 AndAlso column2Import(0) > -1 Then
        If tableAlreadyExist Then
            While csvCopy.ReadNextRecord()
                Dim csvRow As DataRow = datatable.NewRow
                Dim i As Integer = 0
                For Each column As Integer In column2Import
                    Select Case datatable.Columns(i).DataType.Name
                        Case "String"
                            If String.IsNullOrEmpty(csvCopy(column)) Then
                                csvRow(i) = ""
                            Else
                                csvRow(i) = Convert.ToString(csvCopy(column))
                            End If
                        Case "Int32"
                            If String.IsNullOrEmpty(csvCopy(column)) Then
                                csvRow(i) = DBNull.Value
                            Else
                                csvRow(i) = Convert.ToInt32(csvCopy(column))
                            End If
                        Case "Double"
                            If String.IsNullOrEmpty(csvCopy(column)) Then
                                csvRow(i) = DBNull.Value
                            Else
                                csvRow(i) = Convert.ToDouble(csvCopy(column))
                            End If
                        Case "DateTime"
                            If String.IsNullOrEmpty(csvCopy(column)) Then
                                csvRow(i) = Nothing
                            Else
                                csvRow(i) = Convert.ToDateTime(csvCopy(column))
                            End If
                        Case "Boolean"
                            If String.IsNullOrEmpty(csvCopy(column)) Then
                                csvRow(i) = DBNull.Value
                            Else
                                csvRow(i) = Convert.ToBoolean(csvCopy(column))
                            End If
                    End Select
                    i += 1
                Next
                datatable.Rows.Add(csvRow)
            End While
        Else
            For Each column As Integer In column2Import
                datatable.Columns.Add(headers(column))
            Next
            While csvCopy.ReadNextRecord()
                Dim csvRow As DataRow = datatable.NewRow
                Dim i As Integer = 0
                For Each column As Integer In column2Import
                    csvRow(i) = csvCopy(column)
                    i += 1
                Next
                datatable.Rows.Add(csvRow)
            End While
        End If
    Else
        If tableAlreadyExist Then
            While csvCopy.ReadNextRecord()
                Dim csvRow As DataRow = datatable.NewRow
                For i = 0 To csvCopy.FieldCount - 1
                    Select Case datatable.Columns(i).DataType.Name
                        Case "String"
                            If String.IsNullOrEmpty(csvCopy(i)) Then
                                csvRow(i) = ""
                            Else
                                csvRow(i) = Convert.ToString(csvCopy(i))
                            End If
                        Case "Int32"
                            If String.IsNullOrEmpty(csvCopy(i)) Then
                                csvRow(i) = DBNull.Value
                            Else
                                csvRow(i) = Convert.ToInt32(csvCopy(i))
                            End If
                        Case "Double"
                            If String.IsNullOrEmpty(csvCopy(i)) Then
                                csvRow(i) = DBNull.Value
                            Else
                                csvRow(i) = Convert.ToDouble(csvCopy(i))
                            End If
                        Case "DateTime"
                            If String.IsNullOrEmpty(csvCopy(i)) Then
                                csvRow(i) = Nothing
                            Else
                                csvRow(i) = Convert.ToDateTime(csvCopy(i))
                            End If
                        Case "Boolean"
                            If String.IsNullOrEmpty(csvCopy(i)) Then
                                csvRow(i) = DBNull.Value
                            Else
                                csvRow(i) = Convert.ToBoolean(csvCopy(i))
                            End If
                    End Select
                Next
                datatable.Rows.Add(csvRow)
            End While
        Else
            For Each header As String In headers
                datatable.Columns.Add(header)
            Next
            While csvCopy.ReadNextRecord()
                Dim csvRow As DataRow = datatable.NewRow
                For i = 0 To csvCopy.FieldCount - 1
                    csvRow(i) = csvCopy(i)
                Next
                datatable.Rows.Add(csvRow)
            End While
        End If
    End If
End Sub

我错过了什么?

4

1 回答 1

0

这是解决我的问题的代码:

    'Create a DataTable with the same schema of your DataBase
    Dim ccDataTable As CCListDataTable = New CCListDataTable

    'Import here your csvFile into your new DataTable
    Call ImportCSV2Data(dialog2openFile.FileName, ccDataTable, {0, 2}, True)

    'Merge it with the correct DataTable from your Database (False to recognize the changes)
    Me.HQSdbDataSet.CCList.Merge(ccDataTable, False)

    'Then update your DataBase
    Me.CCListBindingSource.EndEdit()
    Me.CCListTableAdapter.Update(Me.HQSdbDataSet.CCList)

如果您在调试模式下关闭应用程序时丢失更改,这是正常的,因为您有两个数据库,一个在您的应用程序中,另一个在调试文件夹中,当您进入调试模式但启动应用程序时,一个会擦除另一个没有调试模式就可以了。 其他解决方案:在您的数据库中选择“从不复制”

于 2013-09-05T11:43:58.770 回答