0

我需要使用 Visual Basic .Net 2010 将逗号分隔的 CSV 文件中的记录导入 SQL Server R2 数据库中的现有表中。表中的现有记录将在导入之前删除。我已经能够创建一个内存中的临时 DataTable 并使用 TextFieldParser 从 CSV 文件填充记录。我通过将内存中的 DataTable 绑定到 DataGridView 来检查它。但我对第二部分一无所知,即如何将记录从内存数据表中插入到 SQL 表中。

我做了以下事情:

`Dim TextFileReader As New TextFieldParser("C:\csvtosql\StockVB\VFPFiles\ExpSysusers.csv")

TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(",")

Dim TextFileTable As DataTable = Nothing

Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()

CurrentRow = TextFileReader.ReadFields() ' Ignore the header
While Not TextFileReader.EndOfData
    Try
        CurrentRow = TextFileReader.ReadFields()

        If Not CurrentRow Is Nothing Then
            ''# Check if DataTable has been created
            If TextFileTable Is Nothing Then
                TextFileTable = New DataTable("TextFileTable")
                ''# Get number of columns
                UpperBound = CurrentRow.GetUpperBound(0)
                ''# Create new DataTable
                For ColumnCount = 0 To UpperBound
                    Column = New DataColumn()
                    Column.DataType = System.Type.GetType("System.String")
                    Column.ColumnName = "Column" & ColumnCount
                    Column.Caption = "Column" & ColumnCount
                    Column.ReadOnly = True
                    Column.Unique = False
                    TextFileTable.Columns.Add(Column)
                Next
            End If

            Row = TextFileTable.NewRow
            For ColumnCount = 0 To UpperBound
                Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
            Next
            TextFileTable.Rows.Add(Row)

        End If

    Catch ex As Exception
        MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
    End Try
End While
TextFileReader.Dispose()
DataGridView1.DataSource = TextFileTable

` 任何人都可以帮助/指导我吗?

我尝试使用以下代码从 DataTable 中读取记录并插入到 SQL 表中。但似乎只添加了第一条记录。

For Each TextFileTableDataRow As DataRow In TextFileTable.Rows
        Dim Column0 As String = TextFileTableDataRow("Column0")
        Dim Column1 As String = TextFileTableDataRow("Column1")
        Dim Column2 As Int16 = TextFileTableDataRow("Column2")
        Dim Column3 As Boolean = TextFileTableDataRow("Column3")

        Dim strSqlQry As String = "INSERT INTO Personnel (Operator,OpPassword,SecurityLevel,Active) VALUES (@Operator,@OpPassword,,@SecurityLevel,@Active)"
        Dim SqlconnectionString As String = gcconnect

        Using connection As New SqlClient.SqlConnection(SqlconnectionString)
            Dim cmd As New SqlClient.SqlCommand(strSqlQry, connection)

            ' create command objects and add parameters
            With cmd.Parameters
                .AddWithValue("@Operator", Column0)
                .AddWithValue("@OpPassword", Column1)
                .AddWithValue("@SecurityLevel", Column3)
                .AddWithValue("@LoggedOn", Column7)
            End With

            Dim adapter As New SqlClient.SqlDataAdapter()
            adapter.InsertCommand = cmd

            '--Update the original SQL table from the datatable
            Dim iRowsInserted As Int32 = adapter.Update(TextFileTable)
        End Using
    Next

我收到以下错误:“违反主键约束‘PK_Personnel’。无法在对象‘dbo.Personnel’中插入重复键。”

4

0 回答 0