简体   繁体   English

使用重复的键更新表适配器

[英]Updating a tableadapter with duplicated keys

I'm new in updating databases and apparently I'm not good in it. 我是更新数据库的新手,显然我不擅长。

Details : 细节 :

  • I use tableadapter (I don't know if this is the best way) to refer to my database in a .mdf file 我使用tableadapter(我不知道这是否是最好的方法)在.mdf文件中引用我的数据库
  • My table has two columns and the first one is the Primary Key 我的表有两列,第一列是Primary Key
  • The table has already some rows 该表已经有一些行
  • I fill my tableadapter with : Me.CCListTableAdapter.Fill(Me.HQSdbDataSet.CCList) 我用: Me.CCListTableAdapter.Fill(Me.HQSdbDataSet.CCList)填充Me.CCListTableAdapter.Fill(Me.HQSdbDataSet.CCList)

What I'm trying to do : 我正在尝试做的是:

  1. I create a new datatable based on my CCListTable : Dim ccDataTable As CCListDataTable = New CCListDataTable 我基于CCListTable创建一个新的数据表: Dim ccDataTable As CCListDataTable = New CCListDataTable
  2. I import a .csv file with new rows (> 3000 lines) using CsvReader into this new datatable. 我使用CsvReader将具有新行(> 3000行)的.csv文件导入此新数据表。 There are rows who already exists into my database 我的数据库中已经存在一些行
  3. I try then to add these new rows into the tableadapter with : 然后,我尝试使用以下命令将这些新行添加到tableadapter中:

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

But it doesn't work and I get an SqlException with Violation of PRIMARY KEY [...] Cannot insert duplicate key . 但是它不起作用,并且我收到一个Violation of PRIMARY KEY [...] Cannot insert duplicate keySqlException Violation of PRIMARY KEY [...] Cannot insert duplicate key Ok like I said I know there are duplicate keys but I don't want to insert these but only update the tableadapter with the new rows. 好的,就像我说的,我知道有重复的键,但是我不想插入这些键,而只用新的行更新tableadapter。

Here the sub that I have created to import my CSV (if it can help) : 这是我创建的用于导入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

What am I missing? 我想念什么?

Here is the code who resolves my issue : 这是解决我的问题的代码:

    '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)

If you loose the changes when you close your application in debug mode it is normal because you have two databases one ine your application and the other in the debug folder and the one erase the other when you enter the debug mode but when you launch your application whitout debug mode it is ok. 如果在调试模式下关闭应用程序时松开更改,那是正常的,因为在应用程序中有两个数据库,一个数据库位于调试文件夹中,另一个数据库在进入调试模式但启动应用程序时会擦除另一个数据库退出调试模式就可以了。 Other solution : In the propriety of you database select 'NEVER COPY' 其他解决方案:在您的数据库中,选择“ NEVER COPY”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM