vb.net中的多线程csv阅读器+插件

时间:2016-05-25 13:44:46

标签: vb.net multithreading csv oledb advantage-database-server

我的想法是启动2个不同的线程,这有点问题,其中一个读取整个文件目录并使用jet oledb插件读取每个文件,另一个称为write的线程,将所有已读入的行插入到sql数据库中。 这是我的全局变量:

  Public Class SQL_Data_Form
    Public getData As DataTable
#Region "Functions"
    Private _table As String = "" 'tabelle für inserts
    Private read, write As Thread
    Private db As New DBUmgebung.cdb
    Private oSQL As New DBUmgebung.cdb.SQL()

    Private Delegate Sub AsyncHandler(ByVal file As String)
    Private AsyncReader As AsyncHandler
    Private AsyncWriter As AsyncHandler

这是触发执行的按钮:

Private Sub btnAusfuehren_Click(sender As System.Object, e As System.EventArgs) Handles btnAusfuehren.Click

    Dim ListPathFiles As List(Of String) = ListFiles.GetFileList(TextBox1.Text, False) 

    For Each filePath As String In ListPathFiles 
        Dim fileInfo As New FileInfo(filePath)

        _table = fileInfo.Name.Replace(fileInfo.Extension, String.Empty)

        Try
            View("connect", 15, "")
            Dim conn As New DBUmgebung.cdb
            View("connection successful", 20, "")
            View("check the table... ", 25, _table)
            If Not conn.executeCommand("SELECT * FROM " & _table) Then
                Throw New Exception("Table missing")
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            rtbSql.Focus()
            View("closed", 100, "")

            Exit Sub
        End Try

        GetCSVFile(filePath)

        AsyncReader = New AsyncHandler(AddressOf GetCSVFile)
        AsyncReader.BeginInvoke(filePath, AddressOf AsyncReaderCallback, Nothing)


        read = New Thread(AddressOf Me.GetCSVFile)
        read.IsBackground = True
        read.Start()

        Thread.Sleep(100)
        InsertCSVFile(filePath)

        If read.IsAlive Then
            write = New Thread(AddressOf Me.InsertCSVFile)
            write.IsBackground = True
            write.Start()
        Else
            Throw New ApplicationException("process read exception")
            rtbSql.Focus()
        End If
    Next
End Sub

它首先列出目录中的所有文件,然后启动线程读取,这里只读取带有oledb插件的所有文件:

Private Function GetCSVFile(ByVal file As String) As DataTable

    Try
        Dim dt As New DataTable
        Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & TextBox1.Text & ";Extended Properties=""TEXT;HDR=Yes;FMT=Delimited"""
        Dim conn As New OleDb.OleDbConnection(ConStr)

        Dim da As New OleDb.OleDbDataAdapter("Select * from " & _table & ".csv", conn)
        da.Fill(dt)
        getData = dt
    Catch ex As OleDbException
        MessageBox.Show(ex.Message)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Return getData
End Function

所以此后它将所有数据传递给线程写入,并将其插入sql数据库中:

Private Sub InsertCSVFile(ByVal filepath As String)

        Dim BigData As DataTable = GetCSVFile(filepath)

        View("read Filetable", 30, "")

        View("inserting... ", 35, "")

        For i = BigData.Rows.Count To 0 Step -1



            Try
                oSQL.init()
                oSQL.ausfuehrenSQLDT(DBUmgebung.cdb.KSQLCommand.INSERT, _table, "", BigData)

                Dim dtRow As DataRow = BigData.NewRow

                BigData.Rows.Add(dtRow)
                DataGridView1.Refresh()
                Application.DoEvents()

            Catch sqlEx As SqlException
                MessageBox.Show(sqlEx.Message)

                rtbSql.Focus()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                rtbSql.Focus()
            End Try
            DataGridView1.DataSource = BigData
            View("closed", 100, "")
        Next
    End Sub

所以我仍然在努力解决如何参数化插入的问题,这是我的第一个想法:

Private Sub GetTypesSQL()
        Dim dtInserts As DataTable = db.GetDataTable("SELECT TOP 0 * FROM " & _table)
        Dim ListOfTypes As New List(Of System.Type)

        For Each _col As DataColumn In dtInserts.Columns
            Dim _type As System.Type = _col.DataType
            ListOfTypes.Add(_type)
        Next
        Dim _wert1 As String = "11.11.2011"
        Dim _type1 As System.Type = ListOfTypes.Item(1)
        If DateTime.TryParse(_wert1, New Date) Then
        End If
    End Sub

它通过查询检查每列有哪种数据类型,但是它有时不足以检查所有数据类型和喷射oledb读取器跳线,有没有人知道这里的主要问题是什么?

0 个答案:

没有答案