Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If InitialINI() = False Then

            WriteLog("檔案" & Application.StartupPath + "\initial.ini" & " 不存在請確認路徑或檔案名稱是否正確")

        End If

 

        Dim myArg() As String

        Dim icount As Integer

        myArg = System.Environment.GetCommandLineArgs

        For icount = 0 To UBound(myArg)

            Select Case Mid(myArg(icount).ToString, 1, 3)

                Case "-am" '上午排程

                    WriteLog("--=====[資料檢查開始]=====--")

                    If Not f_ChackFile() Then

                        s_SendMail("AM")

                    Else

                        WriteLog("[資料已存在]")

                    End If

                    WriteLog("--=====[資料檢查結束]=====--")

                Case "-pm" '下午排程

                    Call s_StartsToWork()

            End Select

        Next

        Me.Close()

    End Sub

_

 

 

    '函式檢查檔案

    Private Function f_ChackFile() As Boolean

        Dim sP_InputString As String = ""     '讀取檔案資料列

        Dim sL_FaildString As String = ""     '檢核訊息

        Dim sL_TypeName As String = ""     '檔案類別

        Dim sL_FilePath As String = ""       '檔案位置

        Dim lL_Success As Integer = 0        '檢核成功筆數

        Dim lL_Faild As Integer = 0          '檢核失敗筆數

        Dim iL_Count As Integer = 0         '檢核筆數

        Dim iL_Chack As Integer = 0         '檢核種類失敗數

 

        sL_TypeName = "[XXX]"

        sL_FilePath = sG_IIDataPath & sG_AviationFileName

        sL_FaildString = ""

        lL_Faild = 0

        iL_Count = 0

        lL_Success = 0

        If File.Exists(sL_FilePath) Then

            WriteLog("找到" & sL_TypeName & "檔案,檢核資料_Starts!")

            Try

                Using sr As StreamReader = New StreamReader(sL_FilePath, myEncoding)

                    sP_InputString = ""

                    Dim x As Long = 0

                    Do

                        x += 1

                        sP_InputString = sr.ReadLine()

                        If Not sP_InputString Is Nothing Then

                            If sP_InputString.Trim <> "" Then

                                Dim sL_Msg As String = ""

                                Dim sL_Premium_Sign As String = Mid(sP_InputString, 106, 1)         'XX1

                                Dim sL_Premium As String = Mid(sP_InputString, 107, 12)             'XX2

                                Dim sL_Pure_Premium_Sign As String = Mid(sP_InputString, 119, 1)    'XX3

                                Dim sL_Pure_Premium As String = Mid(sP_InputString, 120, 12)        'XX4

 

                        Dim bL_ByteData As Byte() = System.Text.Encoding.GetEncoding(0).GetBytes(sP_InputString)

                        Dim myEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(0)

                        Dim iL_ByteLength As Integer = bL_ByteData.Length

                        If iL_ByteLength < 489 Then

                            Array.Resize(bL_ByteData, 489)

                        End If

 

                        Dim sL_CompanyCode As String = SplitBytes(bL_ByteData, 1, 2, myEncoding)

 

                                '訊息處理

                                If sL_Msg <> "" Then

                                    lL_Faild += 1

                                    sL_FaildString += "[" & x.ToString & "]" & sL_Msg & ","

                                Else

                                    lL_Success += 1

                                End If

                                iL_Count += 1

                            End If

                        End If

 

                    Loop Until sP_InputString Is Nothing

                    sr.Close()

                End Using

 

                sL_FaildString += "共檢核" & iL_Count.ToString & "筆資料,"

                sL_FaildString += lL_Success.ToString + "檢核成功,"

                sL_FaildString += lL_Faild.ToString + "檢核有誤!!"

 

                If lL_Faild > 0 Then

                    WriteLog(sL_TypeName & "資料檢核有誤~!:" & sL_FaildString)

                    iL_Chack += 1

                End If

            Catch ex As Exception

                WriteLog(sL_TypeName & "資料檢核有誤~!:" & ex.Message)

                iL_Chack += 1

            End Try

            WriteLog("找到" & sL_TypeName & "檔案!,檢核資料_End!")

        End If

 

 

        If iL_Chack <> 0 Then

            Return False

        Else

            Return True

        End If

    End Function

 

 

    '函式寫Log

    Public Sub WriteLog(ByVal sP_String As String)

        Dim sL_TodayLog As String = Format(Now(), "yyyyMMdd") + ".txt"

        Dim sL_Path As String = Application.StartupPath + "/Log/" + sL_TodayLog

        If File.Exists(sL_Path) Then

            Using sw As StreamWriter = File.AppendText(sL_Path)

                sw.WriteLine(Format(Now(), "yyyyMMdd HH:mm:ss   ") + sP_String)

                sw.Close()

            End Using

        Else

            Using sw As StreamWriter = File.CreateText(sL_Path)

                sw.WriteLine(Format(Now(), "yyyyMMdd HH:mm:ss   ") + sP_String)

                sw.Close()

            End Using

        End If

    End Sub

 

    '函式_SQL資料庫查詢

    Private Function SQLConnection(ByVal sP_SelectString As String) As DataTable

        Dim sL_ConnectionString As String = sG_SQLConnection

        Dim obj_DataAdapter As SqlClient.SqlDataAdapter = New SqlDataAdapter(sP_SelectString, sL_ConnectionString)

        Dim dt As DataTable = New DataTable

        Dim dv As DataView = New DataView

        Try

            obj_DataAdapter.Fill(dt)

            Return dt

        Catch ex As Exception

            WriteLog("ERROR SQLConnection ")

            WriteLog(ex.ToString)

            Return Nothing

        End Try

    End Function

 

    '函式_SQL新增、修改、刪除

    Private Function SQLCommandConnection(ByVal sP_ComdStr As String) As Boolean

        Dim sL_ConnectiongString As String = sG_SQLConnection

        Dim SqlConnect As SqlConnection = New SqlConnection(sL_ConnectiongString)

        Try

            Using SqlConnect

                Dim cmd As SqlCommand = New SqlCommand(sP_ComdStr, SqlConnect)

                SqlConnect.Open()

                cmd.ExecuteNonQuery()

            End Using

            Return True

        Catch ex As Exception

            WriteLog("ERROR SQLCommandConnection ")

            WriteLog(ex.ToString)

            Return False

        End Try

    End Function

 

    ''' <summary>

    ''' Byte區間轉為指定 Encode 字元

    ''' </summary>

    ''' <param></param>

    ''' <param></param>

    ''' <param></param>

    ''' <param></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Public Function SplitBytes(ByVal bP_Bytes As Byte(), ByVal iP_Std As Integer, ByVal iP_Length As Integer, ByVal Enconding As System.Text.Encoding) As String

        Dim sL_ReturnString As String = String.Empty

        Dim bL_GetBytes(iP_Length - 1) As Byte

        Dim y As Integer = 0

        For x As Integer = iP_Std - 1 To iP_Std - 1 + iP_Length - 1

            bL_GetBytes(y) = bP_Bytes(x)

            y += 1

        Next

        sL_ReturnString = Enconding.GetString(bL_GetBytes)

        Return sL_ReturnString

    End Function

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 M 的頭像
    M

    M的部落格

    M 發表在 痞客邦 留言(0) 人氣()