我是更新数据库的新手,显然我不擅长。
细节 :
- 我使用 tableadapter(我不知道这是否是最好的方法)在 .mdf 文件中引用我的数据库
- 我的表有两列,第一列是
Primary Key
- 该表已经有一些行
- 我用以下内容填充我的表格适配器:
Me.CCListTableAdapter.Fill(Me.HQSdbDataSet.CCList)
我正在尝试做的事情:
- 我根据我的 CCListTable 创建了一个新的数据表:
Dim ccDataTable As CCListDataTable = New CCListDataTable
- 我使用 CsvReader 将具有新行(> 3000 行)的 .csv 文件导入到这个新数据表中。我的数据库中已经存在一些行
然后我尝试将这些新行添加到 tableadapter 中:
Me.CCListBindingSource.EndEdit() Me.CCListTableAdapter.Update(ccDataTable)
但它不起作用,我得到一个SqlException
with 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
我错过了什么?