Analytics

2010年5月21日 星期五

[VB.NET]使用 StreamReader組字串 讀取csv檔案進行資料匯入處理(Use StreamReader group carried out a string read csv file data import process)


問題
使用 StreamReader組字串 讀取csv檔案進行資料匯入處理



解決方法
//取得目前程式執行的位置
Dim Parent_LogPath As String
Parent_LogPath = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).ToString

//取得執行位置底下資料夾的csv檔(此csv檔以,分割像aaaaaa,bbbbb,ccccc)
Dim sr As StreamReader = New StreamReader(Parent_LogPath &; "\data\" & "test0520.csv")
Dim _str1 As String = ""
Dim _str2 As String = ""
Dim _str3 As String = ""
Dim _strSplit As Array
Dim _readcount As Integer = 0
Dim _Totalcount As Integer = 0

//未讀到最後一筆資料就繼續執行
While sr.EndOfStream <> True 
    _strSplit = Split(sr.ReadLine(), ","
    _str1 = _str1 & _strSplit(0) & ","
    _str2 = _str2 & _strSplit(1) & ","
    _str3 = _str3 & _strSplit(2) & ","

    //組1千筆資料後執行一次
    If (_readcount = = 1000) Then
        _str1 = _str1.Substring(0, _str1.Length - 1)
        _str2 = _str2.Substring(0, _str2.Length - 1)
        _str3 = _str3.Substring(0, _str3.Length - 1)
        ToWriteLog(_str1, _str2, _str3)

    //調用Imports System.Threading
    //跑一次後休息3分鐘,1秒=1000
    Thread.Sleep(180000)
    //重新歸初始值
    _readcount = 0
    _str1 = ""
    _str2 = ""
    _str3 = ""

    End If

_readcount = _readcount + 1
_Totalcount = _Totalcount + 1

 End While

//剩下未滿1千筆的資料再執行一次
If _readcount < = 1000 Then
ToWriteLog(_str1, _str2, _str3)

End If

沒有留言:

熱門文章