2016-05-13 7 views
3

私は短期間でcsvの100万個のデータセットを処理するはずのプログラムを書いています。私の考えはパフォーマンス上の理由からodbcを使用することでした。したがって、すべてのデータをodbcメモリに保存し、その後、私はパラメータを追加し、SQL DBに挿入し、ここに私のコードは、これまでのところです:vb.netのcsvのためのodbcリーダー

Using connection As New OdbcConnection("jdbc:odbc:Driver={Microsoft Text Driver (*.txt; *.csv)};" & filePath & "Extensions=csv;Persist Security Info=False;") 
       Dim reader As OdbcDataReader 

       Dim i As Integer 
       Dim r As SeekZeilen 

       Dim TextFileTable As DataTable = Nothing 


       Dim line As String = reader.Read() 
       Me.ParseString(line) 

       Dim memStream As New MemoryStream(Encoding.Default.GetBytes(line)) 

       Using TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(memStream) 
        TextFileReader.TextFieldType = FileIO.FieldType.Delimited 
        TextFileReader.SetDelimiters(";") 
        r.erste_Zeile = TextFileReader.ReadFields() 

        If TextFileTable Is Nothing Then 
         TextFileTable = New DataTable("TextFileTable") 

         For i = 0 To r.erste_Zeile.Length - 1 
          Dim Column As New DataColumn(r.erste_Zeile(i)) 

          Column.ReadOnly = True 
          TextFileTable.Columns.Add(Column) 
         Next 
        End If 
        DataGridView1.DataSource = TextFileTable 
       End Using 

       While reader.HasRows 
        line = reader.Read() 
        Me.ParseString(line) 
        memStream = New MemoryStream(Encoding.Default.GetBytes(line)) 

        Using TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(memStream) 
         TextFileReader.TextFieldType = FileIO.FieldType.Delimited 
         TextFileReader.SetDelimiters(";") 

         DataGridView1.DataSource = TextFileTable 
         Try 
          r._Rest = TextFileReader.ReadFields() 


          ReplaceChars(r._Rest) 

          If Not r._Rest Is Nothing Then 
           Dim oSQL As New DBUmgebung.cdb.SQL() 
           oSQL.init() 
           AddParameters(oSQL, r) 
           oSQL.ausfuehrenSQL(DBUmgebung.cdb.KSQLCommand.INSERT, _table, "") 
           Dim dtRow As DataRow = TextFileTable.NewRow 

           For i = 0 To r._Rest.Length - 1 

            dtRow(i) = r._Rest(i).ToString() 
           Next 

           TextFileTable.Rows.Add(dtRow) 
           DataGridView1.Refresh() 
           Application.DoEvents() 
          End If 
         Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException 
          MsgBox("Error! " & ex.Message & _ 
          "") 

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

          rtbSql.Focus() 
          Exit For 
         Catch ex As Exception 
          MessageBox.Show(ex.Message) 
          rtbSql.Focus() 
          Exit For 

         End Try 
        End Using 
       End While 
       reader.Close() 
      End Using 

問題は、私は未知の理由でnullポインタ例外を取得することである、誰もが私の考えを持っていません間違った?私のODBCリーダーが正しく初期化されていないためでしょうか?

+0

私はこのようなことを一度覚えています。私が調査したことから、OleDbConnectionはすべてのテキストとしてExcelファイルを読み込みます。それ以外の場合は、今説明した問題に取り組むことができます。 – codeMonger123

+0

odbccommandを「Dim cmd As New OdbcCommand'で初期化し、それを 'reader = cmd.ExecuteReader()'を実行して修正しようとしましたが、無効操作例外が発生しました – Sparkm4n

答えて

3

これを試してください。これはcsvファイルをすべてのテキストとしてデータテーブルに読み込みます。 Datatableに入ったら、そのレコードをSQLに挿入することができます。複数のcsvファイルを扱う場合は、これを常に調整できます。

Friend Shared Function GetExcelFile(ByVal strFileName As String, ByVal strPath As String) As DataTable 

    Try 

     Dim dt As New DataTable 

      Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""Text;HDR=Yes;FMT=Delimited\""" 
      Dim conn As New OleDb.OleDbConnection(ConStr) 
      Dim da As New OleDb.OleDbDataAdapter("Select * from " & strFileName, conn) 
      da.Fill(dt) 

     Return dt 

    Catch ex As Exception 
     Return Nothing 
    End Try 

End Function 
+0

あなたの質問に答えるかどうか教えてください。ありがとう – codeMonger123

+0

スパークム - このポストの接続文字列パラメータに注意してください。これは元の問題を解決するかもしれません。 – rheitzman

+1

まあ、私は最初にSQLを使わずに普通のCSV文書を読む方法を知る必要があります。最近の記事で読んだように、odbcは大きなcsvファイルを読み込むための最も効果的な方法ですが、odbcを使用する簡単な方法はありますか? – Sparkm4n

関連する問題